-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathPermutation in String.java
More file actions
38 lines (37 loc) · 1.18 KB
/
Permutation in String.java
File metadata and controls
38 lines (37 loc) · 1.18 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
class Solution {
public boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length()) {
return false;
}
Map<Character, Integer> map = new HashMap<>();
for (char c : s1.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
int n = s1.length();
int[] count = {map.size()};
for (int i = 0; i < n - 1; i++) {
updateMap(map, s2.charAt(i), count, true);
}
int start = 0;
for (int i = n - 1; i < s2.length(); i++) {
updateMap(map, s2.charAt(i), count, true);
if (count[0] == 0) {
return true;
}
updateMap(map, s2.charAt(start++), count, false);
}
return false;
}
private void updateMap(Map<Character, Integer> map, char c, int[] count, boolean decrement) {
if (map.containsKey(c)) {
int diff = decrement ? -1 : 1;
map.put(c, map.get(c) + diff);
if (decrement && map.get(c) == 0) {
count[0]--;
}
if (!decrement && map.get(c) == 1) {
count[0]++;
}
}
}
}