Problem
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string"s permutations is the substring of the second string.
ExampleExample 1:
Input:s1 = "ab" s2 = "eidbaooo"
Output:True
Explanation: s2 contains one permutation of s1 ("ba").
Example 2:
Input:s1= "ab" s2 = "eidboaoo"
Output: False
public class Solution { /** * @param s1: a string * @param s2: a string * @return: if s2 contains the permutation of s1 */ public boolean checkInclusion(String s1, String s2) { int len = s1.length(); for (int i = 0; i <= s2.length()-len; i++) { if (isPermutation(s2.substring(i, i+len), s1)) return true; } return false; } //use the method of #String Permutation public boolean isPermutation(String s1, String s2) { if (s1 == null) return s2 == null; if (s2 == null) return s1 == null; if (s1.length() != s2.length()) return false; int[] dict = new int[256]; char[] c1 = s1.toCharArray(); char[] c2 = s2.toCharArray(); for (char ch: c1) { dict[(int)ch]++; } for (char ch: c2) { dict[(int)ch]--; if (dict[(int)ch] < 0) return false; } return true; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/69721.html
摘要:從末位向前遍歷,假設循環開始全是倒序排列,如當第一次出現正序的時候,如的和此時從數列末尾向前循環到,找到第一個比大的交換這兩個數,變成倒置第位到末位的數為正序排列這里的是完全倒置的排列,如,即上面循環的情況完全沒有出現, Problem Implement next permutation, which rearranges numbers into the lexicographic...
摘要:我覺得雖然在里分類是,但其實是一道難題。思路如下搞一個哈希表,存儲數組中每一位的后面小于它的數的個數。與上一題的不同之處時會有重復的數。當然,每個重復數的都要階乘,例如有個,個,就是。是所有排列的次數和,返回下一次。 Permutation Index Problem Given a permutation which contains no repeated number, find...
Problem Given a list of integers, which denote a permutation. Find the previous permutation in ascending order. Notice The list may contains duplicate integers. Example For [1,3,2,3], the previous per...
摘要:做法先把這個數放入一個數組里,同時計算出的階乘。假設這一組是第組,第一個數就是,同時刪去這個數,并讓除以取余作為新的。如此循環,這樣,下一組的成員數減少了,要找的位置也更為精確了。 Problem Given n and k, return the k-th permutation sequence. Example For n = 3, all permutations are li...
Problem Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first strings permutations is the substring of the second string.E...
閱讀 3250·2023-04-25 22:47
閱讀 3765·2021-10-11 10:59
閱讀 2300·2021-09-07 10:12
閱讀 4243·2021-08-11 11:15
閱讀 3432·2019-08-30 13:15
閱讀 1750·2019-08-30 13:00
閱讀 968·2019-08-29 14:02
閱讀 1680·2019-08-26 13:57