Similar problems:
LeetCode 438 - Find All Anagrams in a String
LeetCode 30 - Substring with Concatenation of All Words
Longest Substring with at most 2 Distinct Characters
Longest Substring with at most k Distinct Characters
https://leetcode.com/problems/permutation-in-string
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.
Example 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
Note:
- The input strings only contain lower case letters.
- The length of both given strings is in range [1, 10,000].
Let's take a look at a solution using sliding window in Find All Anagrams in a String( https://leetcode.com/problems/find-all-anagrams-in-a-string/#/description )
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<Integer>();
if(s == null || s.length() == 0 || p == null || p.length() == 0) return res;
int begin = 0, end = 0;
Map<Character, Integer> map = new HashMap<>();
for(char c : p.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
int counter = map.size();
while(end < s.length()){
char ch = s.charAt(end);
if(map.containsKey(ch)) {
map.put(ch, map.get(ch) - 1);
if(map.get(ch) == 0) {
counter--;
}
}
while(counter == 0) {
if(end - begin + 1 == p.length()) {
res.add(begin);
}
char temp = s.charAt(begin);
if(map.containsKey(temp)) {
map.put(temp, map.get(temp) + 1);
if(map.get(temp) > 0) {
counter++;
}
}
begin++;
}
end++;
}
return res;
}
Then is my solution for this problem, it's a little bit long but this template can be applied to a lot of problems.
public boolean checkInclusion(String s1, String s2) {
if(s1 == null || s2 == null) {
return false;
}
int len = s1.length();
Map<Character, Integer> map = new HashMap<>();
for(char c : s1.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
int count = map.size();
int begin = 0;
int end = 0;
while(end < s2.length()) {
char ch = s2.charAt(end);
if(map.containsKey(ch)) {
map.put(ch, map.get(ch) - 1);
if(map.get(ch) == 0) {
count--;
}
}
while(count == 0) {
if(end - begin + 1 == len) {
return true;
}
char temp = s2.charAt(begin);
if(map.containsKey(temp)) {
map.put(temp, map.get(temp) + 1);
if(map.get(temp) > 0) {
count++;
}
}
begin++;
}
end++;
}
return false;
}
X. http://www.cnblogs.com/grandyang/p/6815227.html这道题的正确做法应该是使用滑动窗口Sliding Window的思想来做,可以使用两个哈希表来做,或者是使用一个哈希表配上双指针来做。我们先来看使用两个哈希表来做的情况,我们先来分别统计s1和s2中前n1个字符串中各个字符出现的次数,其中n1为字符串s1的长度,这样如果二者字符出现次数的情况完全相同,说明s1和s2中前n1的字符互为全排列关系,那么符合题意了,直接返回true。如果不是的话,那么我们遍历s2之后的字符,对于遍历到的字符,对应的次数加1,由于窗口的大小限定为了n1,所以每在窗口右侧加一个新字符的同时就要在窗口左侧去掉一个字符,每次都比较一下两个哈希表的情况,如果相等,说明存在
bool checkInclusion(string s1, string s2) { int n1 = s1.size(), n2 = s2.size(); vector<int> m1(128), m2(128); for (int i = 0; i < n1; ++i) { ++m1[s1[i]]; ++m2[s2[i]]; } if (m1 == m2) return true; for (int i = n1; i < n2; ++i) { ++m2[s2[i]]; --m2[s2[i - n1]]; if (m1 == m2) return true; } return false; }
https://discuss.leetcode.com/topic/87845/java-solution-sliding-window
http://www.jianshu.com/p/cb3073eb8838
- How do we know string
p
is a permutation of strings
? Easy, each character inp
is ins
too. So we can abstract all permutation strings ofs
to a map (Character -> Count). i.e.abba
->{a:2, b:2}
. Since there are only 26 lower case letters in this problem, we can just use an array to represent the map. - How do we know string
s2
contains a permutation ofs1
? We just need to create a sliding window with length ofs1
, move from beginning to the end ofs2
. When a character moves in from right of the window, we subtract1
to that character count from the map. When a character moves out from left of the window, we add1
to that character count. So once we see all zeros in the map, meaning equal numbers of every characters betweens1
and the substring in the sliding window, we know the answer is true.
public boolean checkInclusion(String s1, String s2) {
int len1 = s1.length(), len2 = s2.length();
if (len1 > len2) return false;
int[] count = new int[26];
for (int i = 0; i < len1; i++) {
count[s1.charAt(i) - 'a']++;
count[s2.charAt(i) - 'a']--;
}
if (allZero(count)) return true;
for (int i = len1; i < len2; i++) {
count[s2.charAt(i) - 'a']--;
count[s2.charAt(i - len1) - 'a']++;
if (allZero(count)) return true;
}
return false;
}
private boolean allZero(int[] count) {
for (int i = 0; i < 26; i++) {
if (count[i] != 0) return false;
}
return true;
}
X.下面这种解法是利用一个哈希表加上双指针,我们还是先统计s1中字符的出现次数,然后遍历s2中的字符,对于每个遍历到的字符,我们在哈希表中对应的字符次数减1,如果次数次数小于0了,说明该字符在s1中不曾出现,或是出现的次数超过了s1中的对应的字符出现次数,那么我们此时移动滑动窗口的左边界,对于移除的字符串,哈希表中对应的次数要加1,如果此时次数不为0,说明该字符不在s1中,继续向右移,直到更新后的次数为0停止,此时到达的字符是在s1中的。如果次数大于等于0了,我们看此时窗口大小是否为s1的长度,若二者相等,由于此时窗口中的字符都是在s1中存在的字符,而且对应的次数都为0了,说明窗口中的字符串和s1互为全排列,返回true即可
bool checkInclusion(string s1, string s2) { int n1 = s1.size(), n2 = s2.size(), left = 0; vector<int> m(128); for (char c : s1) ++m[c]; for (int right = 0; right < n2; ++right) { if (--m[s2[right]] < 0) { while (++m[s2[left++]] != 0) {} } else if (right - left + 1 == n1) return true; } return n1 == 0; }
X.
下面这种解法也是用一个哈希表外加双指针来做的,跟上面的解法思路大体相同,写法有些不同,不变的还是统计s1中字符出现的次数,不一样的是我们用一个变量cnt来表示还需要匹配的s1中的字符的个数,初始化为s1的长度,然后遍历s2中的字符,如果该字符在哈希表中存在,说明匹配上了,cnt自减1,哈希表中的次数也应该自减1,然后如果cnt减为0了,说明s1的字符都匹配上了,如果此时窗口的大小正好为s1的长度,那么说明找到了s1的全排列,返回true,否则说明窗口过大,里面有一些非s1中的字符,我们将左边界右移,同时将移除的字符串在哈希表中的次数自增1,如果增加后的次数大于0了,说明该字符是s1中的字符,我们将其移除了,那么cnt就要自增1
bool checkInclusion(string s1, string s2) { int n1 = s1.size(), n2 = s2.size(), cnt = n1, left = 0; vector<int> m(128); for (char c : s1) ++m[c]; for (int right = 0; right < n2; ++right) { if (m[s2[right]]-- > 0) --cnt; while (cnt == 0) { if (right - left + 1 == n1) return true; if (++m[s2[left++]] > 0) ++cnt; } } return false; }
https://discuss.leetcode.com/topic/87868/java-solution-two-pointers
public boolean checkInclusion(String s1, String s2) {
if(s1.length()>s2.length()) return false;
int[] alphabets = new int[26];
int len =s1.length();
for(char ch : s1.toCharArray()){
alphabets[ch-'a']++;
}
int cnt =len;
char[] sArr = s2.toCharArray();
int start =0;
int end =0;
while(end<sArr.length){
if(alphabets[sArr[end++]-'a']-->0) cnt--;
while(cnt==0){
// System.out.println(end+","+start);
if(end-start == len) return true;
if(alphabets[sArr[start++]-'a']++==0) cnt++;
}
}
return false;
}
https://discuss.leetcode.com/topic/87861/c-java-clean-code-with-explanation
public boolean checkInclusion(String s1, String s2) {
char[] ca1 = s1.toCharArray(), ca2 = s2.toCharArray();
int[] cnts = new int[256];
for (char ch : ca1) cnts[ch]++;
int left = ca1.length;
for (int i = 0, j = 0; j < ca2.length; j++) {
if (cnts[ca2[j]]-- > 0) left--;
while (left == 0) {
if (j + 1 - i == ca1.length) return true;
if (++cnts[ca2[i++]] > 0) left++;
}
}
return false;
}
https://discuss.leetcode.com/topic/87884/8-lines-slide-window-solution-in-java public boolean checkInclusion(String s1, String s2) {
int[] count = new int[128];
for(int i = 0; i < s1.length(); i++) count[s1.charAt(i)]--;
for(int l = 0, r = 0; r < s2.length(); r++) {
if (++count[s2.charAt(r)] > 0)
while(--count[s2.charAt(l++)] != 0) { /* do nothing */}
else if ((r - l + 1) == s1.length()) return true;
}
return false;
}
http://bookshadow.com/weblog/2017/04/30/leetcode-permutation-in-string/
滑动窗口(Sliding Window) 时间复杂度O(n)
由于输入只包含小写字母,因此可以通过统计字母个数判断字符串是否互为对方的排列,其时间复杂度为O(1)。
X. https://leetcode.com/articles/short-permutation-in-a-long-string/
Approach #6 Optimized Sliding Window [Accepted]:
The last approach can be optimized, if instead of comparing all the elements of the hashmaps for every updated corresponding to every window of considered, we keep a track of the number of elements which were already matching in the earlier hashmap and update just the count of matching elements when we shift the window towards the right.
To do so, we maintain a variable, which stores the number of characters(out of the 26 alphabets), which have the same frequency of occurence in and the current window in . When we slide the window, if the deduction of the last element and the addition of the new element leads to a new frequency match of any of the characters, we increment the by 1. If not, we keep the intact. But, if a character whose frequency was the same earlier(prior to addition and removal) is added, it now leads to a frequency mismatch which is taken into account by decrementing the same variable. If, after the shifting of the window, the evaluates to 26, it means all the characters match in frequency totally. So, we return a True in that case immediately
- Time complexity : . where is the length of string and is the length of string .
public boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length())
return false;
int[] s1map = new int[26];
int[] s2map = new int[26];
for (int i = 0; i < s1.length(); i++) {
s1map[s1.charAt(i) - 'a']++;
s2map[s2.charAt(i) - 'a']++;
}
int count = 0;
for (int i = 0; i < 26; i++)
if (s1map[i] == s2map[i])
count++;
for (int i = 0; i < s2.length() - s1.length(); i++) {
int r = s2.charAt(i + s1.length()) - 'a', l = s2.charAt(i) - 'a';
if (count == 26)
return true;
s2map[r]++;
if (s2map[r] == s1map[r])
count++;
else if (s2map[r] == s1map[r] + 1)
count--;
s2map[l]--;
if (s2map[l] == s1map[l])
count++;
else if (s2map[l] == s1map[l] - 1)
count--;
}
return count == 26;
}
public boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length())
return false;
int[] s1map = new int[26];
int[] s2map = new int[26];
for (int i = 0; i < s1.length(); i++) {
s1map[s1.charAt(i) - 'a']++;
s2map[s2.charAt(i) - 'a']++;
}
for (int i = 0; i < s2.length() - s1.length(); i++) {
if (matches(s1map, s2map))
return true;
s2map[s2.charAt(i + s1.length()) - 'a']++;
s2map[s2.charAt(i) - 'a']--;
}
return matches(s1map, s2map);
}
public boolean matches(int[] s1map, int[] s2map) {
for (int i = 0; i < 26; i++) {
if (s1map[i] != s2map[i])
return false;
}
return true;
}
- Time complexity : , where is the length of string and is the length of string .
- Space complexity : . and of size 26 is used.
public boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length())
return false;
int[] s1map = new int[26];
for (int i = 0; i < s1.length(); i++)
s1map[s1.charAt(i) - 'a']++;
for (int i = 0; i <= s2.length() - s1.length(); i++) {
int[] s2map = new int[26];
for (int j = 0; j < s1.length(); j++) {
s2map[s2.charAt(i + j) - 'a']++;
}
if (matches(s1map, s2map))
return true;
}
return false;
}
public boolean matches(int[] s1map, int[] s2map) {
for (int i = 0; i < 26; i++) {
if (s1map[i] != s2map[i])
return false;
}
return true;
}
Approach #2 Using sorting [Time Limit Exceeded]:- Time complexity : . where is the length of string and is the length of string .
- Space complexity : . array is used .
public boolean checkInclusion(String s1, String s2) {
s1 = sort(s1);
for (int i = 0; i <= s2.length() - s1.length(); i++) {
if (s1.equals(sort(s2.substring(i, i + s1.length()))))
return true;
}
return false;
}
public String sort(String s) {
char[] t = s.toCharArray();
Arrays.sort(t);
return new String(t);
}
- Time complexity : . We match all the permutations of the short string , of length , with . Here, refers to the length of .
- Space complexity : . The depth of the recursion tree is ( refers to the length of the short string ). Every node of the recursion tree contains a string of max. length .
boolean flag = false;
public boolean checkInclusion(String s1, String s2) {
permute(s1, s2, 0);
return flag;
}
public String swap(String s, int i0, int i1) {
if (i0 == i1)
return s;
String s1 = s.substring(0, i0);
String s2 = s.substring(i0 + 1, i1);
String s3 = s.substring(i1 + 1);
return s1 + s.charAt(i1) + s2 + s.charAt(i0) + s3;
}
void permute(String s1, String s2, int l) {
if (l == s1.length()) {
if (s2.indexOf(s1) >= 0)
flag = true;
} else {
for (int i = l; i < s1.length(); i++) {
s1 = swap(s1, l, i);
permute(s1, s2, l + 1);
s1 = swap(s1, l, i);
}
}
}