forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path567-Permutation-in-String.java
More file actions
34 lines (32 loc) · 940 Bytes
/
567-Permutation-in-String.java
File metadata and controls
34 lines (32 loc) · 940 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
27
28
29
30
31
32
33
34
class Solution {
public boolean checkInclusion(String s1, String s2) {
if (s2.length() < s1.length()) return false;
int[] arr = new int[26];
//add the values to the hash array
for (int i = 0; i < s1.length(); i++) {
arr[s1.charAt(i) - 'a']++;
}
int i = 0;
int j = 0;
//point j to it's position
for (; j < s1.length(); j++) {
arr[s2.charAt(j) - 'a']--;
}
j--;
if (isEmpty(arr)) return true;
while (j < s2.length()) {
arr[s2.charAt(i) - 'a']++;
i++;
j++;
if (j < s2.length()) arr[s2.charAt(j) - 'a']--;
if (isEmpty(arr)) return true;
}
return isEmpty(arr);
}
public boolean isEmpty(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) return false;
}
return true;
}
}