-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path567.java
More file actions
26 lines (25 loc) · 829 Bytes
/
567.java
File metadata and controls
26 lines (25 loc) · 829 Bytes
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
public class Solution {
public boolean check(int[] a, int[] b) {
for (int i = 0; i < 26; i++) {
if (a[i] != b[i]) return false;
}
return true;
}
public boolean checkInclusion(String s1, String s2) {
int[] count1 = new int[26];
int[] count2 = new int[26];
for (char c : s1.toCharArray()) count1[c - 'a']++;
if (s2.length() < s1.length()) return false;
int i;
int start = 0;
for (i = 0; i < s1.length(); i++) count2[s2.charAt(i) - 'a']++;
while (i < s2.length()) {
if (check(count1, count2)) return true;
count2[s2.charAt(start) - 'a']--;
count2[s2.charAt(i) - 'a']++;
i++; start++;
}
if (check(count1, count2)) return true;
return false;
}
}