Skip to content

Commit a3531f9

Browse files
committed
Solved a problem
1 parent c4f1640 commit a3531f9

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Medium/Permutation in String.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public boolean checkInclusion(String s1, String s2) {
3+
int l1 = s1.length();
4+
int l2 = s2.length();
5+
if (l1 > l2) return false;
6+
7+
int[] check = new int[26];
8+
9+
for (int i=0;i<l1;i++) {
10+
check[s1.charAt(i)-'a']++;
11+
check[s2.charAt(i)-'a']--;
12+
}
13+
14+
if (zeroArr(check)) return true;
15+
16+
for (int i = l1; i < l2; i++) {
17+
check[s2.charAt(i) - 'a']--;
18+
check[s2.charAt(i - l1) - 'a']++;
19+
if (zeroArr(check)) return true;
20+
}
21+
22+
return false;
23+
}
24+
25+
boolean zeroArr(int[] arr) {
26+
for (int i=0;i<26;i++) {
27+
if (arr[i] != 0) return false;
28+
}
29+
return true;
30+
}
31+
}

0 commit comments

Comments
 (0)