-
Notifications
You must be signed in to change notification settings - Fork 0
/
567_permutation_in_string.java
50 lines (41 loc) · 1.04 KB
/
567_permutation_in_string.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
// 567. Permutation in String
// Difficulty: Medium
// Topics: Hash Table, Two Pointers, Sliding Window
class Solution {
int[] c1 = new int[26];
int[] c2 = new int[26];
public boolean eqP() {
for (int i = 0; i < 26; i++) {
if (c1[i] != c2[i]) {
return false;
}
}
return true;
}
public boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length()) {
return false;
}
int n1 = s1.length();
int n2 = s2.length();
for (int i = 0; i < 26; i++) {
c1[i] = 0;
c2[i] = 0;
}
for (int i = 0; i < n1; i++) {
c1[s1.charAt(i) - 'a']++;
c2[s2.charAt(i) - 'a']++;
}
if (eqP()) {
return true;
}
for (int i = n1; i < n2; i++) {
c2[s2.charAt(i) - 'a']++;
c2[s2.charAt(i - n1) - 'a']--;
if (eqP()) {
return true;
}
}
return false;
}
}