-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
567.java
53 lines (52 loc) · 2.02 KB
/
567.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
__________________________________________________________________________________________________
sample 2 ms submission
class Solution {
public boolean checkInclusion(String s1, String s2) {
int[] countS = new int[26];
for(int i=0;i<s1.length();i++) countS[s1.charAt(i)-'a']++;
return help(s1, s2, 0, countS);
}
public boolean help(String s1, String s2, int index, int[] countS){
int[] count = new int[26];
int cou = 0;
int slow = index;
for(int i=index;i<s2.length();i++){
if(countS[s2.charAt(i)-'a']==0) return help(s1, s2, i+1, countS);
count[s2.charAt(i)-'a']++;
if(count[s2.charAt(i)-'a']<=countS[s2.charAt(i)-'a']) cou++;
else{
while(count[s2.charAt(i)-'a']>countS[s2.charAt(i)-'a']){
char cur = s2.charAt(slow++);
count[cur-'a']--;
if(count[cur-'a']<countS[cur-'a']) cou--;
}
}
if(cou==s1.length()) return true;
}
return false;
}
}
__________________________________________________________________________________________________
sample 35992 kb submission
class Solution {
public boolean checkInclusion(String s1, String s2) {
Map<Character, Integer> charFreq = getCharacterCount(s1);
int i = 0, j = s1.length();
while (i < (s2.length() - s1.length() + 1)) {
Map<Character, Integer> candidateFreq = getCharacterCount(s2.substring(i, i + s1.length()));
if (charFreq.equals(candidateFreq)) {
return true;
}
i++;
}
return false;
}
private Map<Character, Integer> getCharacterCount(String s) {
Map<Character, Integer> charFreq = new HashMap<>();
for (char c : s.toCharArray()) {
charFreq.merge(c, 1, Integer::sum);
}
return charFreq;
}
}
__________________________________________________________________________________________________