-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPermString.java
More file actions
53 lines (40 loc) · 1.26 KB
/
Copy pathPermString.java
File metadata and controls
53 lines (40 loc) · 1.26 KB
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
import java.util.Arrays;
class PermString{
public static boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length() ) {
return false;
}
int[] s1map = new int[26];
int[] s2map = new int[26];
for (int i = 0; i < s1.length(); i++) {
s1map[s1.charAt(i) - 'a']++;
s2map[s2.charAt(i) - 'a']++;
}
for (int i = 0; i < s2.length() - s1.length(); i++) {
if (matches(s1map, s2map)) {
return true;
}
s2map[s2.charAt(i + s1.length()) - 'a']++;
s2map[s2.charAt(i) - 'a']--;
}
return matches(s1map, s2map);
}
public static void dsp(int[] s1map) {
for (int i = 0; i < s1map.length; i++) {
System.out.print(s1map[i] + " ");
}
System.out.println();
}
public static boolean matches(int[] s1map, int[] s2map) {
for (int i = 0; i < s1map.length; i++) {
if (s1map[i] != s2map[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
assert checkInclusion("ab", "eidbaooo") == true;
assert checkInclusion("ab", "eidboaoo") == false;
}
}