-
Notifications
You must be signed in to change notification settings - Fork 43
/
PermutationInString.java
257 lines (226 loc) · 7.75 KB
/
PermutationInString.java
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package LeetCodeJava.TwoPointer;
// https://leetcode.com/problems/permutation-in-string/
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class PermutationInString {
// V0
// TODO implement
// V0'
// IDEA : COUNTER : count element in string (gpt)
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Two_Pointers/permutation-in-string.py
public boolean checkInclusion_0(String s1, String s2) {
if (s1.length() > s2.length()) {
return false;
}
// Create frequency maps for s1 and the sliding window in s2
Map<Character, Integer> mapS1 = new HashMap<>();
Map<Character, Integer> mapS2 = new HashMap<>();
for (char c : s1.toCharArray()) {
mapS1.put(c, mapS1.getOrDefault(c, 0) + 1);
}
int left = 0;
for (int right = 0; right < s2.length(); right++) {
char rightChar = s2.charAt(right);
mapS2.put(rightChar, mapS2.getOrDefault(rightChar, 0) + 1);
/** NOTE !!!
*
* we simply check permutation with below trick
* -> e.g. if 2 map are equal
*/
// Check if the current window matches the frequency map of s1
if (mapS1.equals(mapS2)) {
return true;
}
// If the window size exceeds the size of s1, move the left pointer
if (right - left + 1 >= s1.length()) {
char leftChar = s2.charAt(left);
mapS2.put(leftChar, mapS2.get(leftChar) - 1);
if (mapS2.get(leftChar) == 0) {
mapS2.remove(leftChar);
}
left++;
}
}
return false;
}
// V1
// IDEA: BRUTE FORCE
// https://leetcode.com/problems/permutation-in-string/editorial/
boolean flag = false;
public boolean checkInclusion_2(String s1, String s2) {
permute(s1, s2, 0);
return flag;
}
public String swap(String s, int i0, int i1) {
if (i0 == i1)
return s;
String s1 = s.substring(0, i0);
String s2 = s.substring(i0 + 1, i1);
String s3 = s.substring(i1 + 1);
return s1 + s.charAt(i1) + s2 + s.charAt(i0) + s3;
}
void permute(String s1, String s2, int l) {
if (l == s1.length()) {
if (s2.indexOf(s1) >= 0)
flag = true;
} else {
for (int i = l; i < s1.length(); i++) {
s1 = swap(s1, l, i);
permute(s1, s2, l + 1);
s1 = swap(s1, l, i);
}
}
}
// V2
// IDEA : SORTING
// https://leetcode.com/problems/permutation-in-string/editorial/
public boolean checkInclusion_3(String s1, String s2) {
s1 = sort(s1);
for (int i = 0; i <= s2.length() - s1.length(); i++) {
if (s1.equals(sort(s2.substring(i, i + s1.length()))))
return true;
}
return false;
}
public String sort(String s) {
char[] t = s.toCharArray();
Arrays.sort(t);
return new String(t);
}
// V3
// IDEA : Hashmap
// https://leetcode.com/problems/permutation-in-string/editorial/
public boolean checkInclusion_4(String s1, String s2) {
if (s1.length() > s2.length())
return false;
HashMap<Character, Integer> s1map = new HashMap<>();
for (int i = 0; i < s1.length(); i++)
s1map.put(s1.charAt(i), s1map.getOrDefault(s1.charAt(i), 0) + 1);
for (int i = 0; i <= s2.length() - s1.length(); i++) {
HashMap<Character, Integer> s2map = new HashMap<>();
for (int j = 0; j < s1.length(); j++) {
s2map.put(s2.charAt(i + j), s2map.getOrDefault(s2.charAt(i + j), 0) + 1);
}
if (matches(s1map, s2map))
return true;
}
return false;
}
public boolean matches(HashMap<Character, Integer> s1map, HashMap<Character, Integer> s2map) {
for (char key : s1map.keySet()) {
if (s1map.get(key) - s2map.getOrDefault(key, -1) != 0)
return false;
}
return true;
}
// V4
// IDEA : Array
// https://leetcode.com/problems/permutation-in-string/editorial/
public boolean checkInclusion_5(String s1, String s2) {
if (s1.length() > s2.length())
return false;
int[] s1map = new int[26];
for (int i = 0; i < s1.length(); i++)
s1map[s1.charAt(i) - 'a']++;
for (int i = 0; i <= s2.length() - s1.length(); i++) {
int[] s2map = new int[26];
for (int j = 0; j < s1.length(); j++) {
s2map[s2.charAt(i + j) - 'a']++;
}
if (matches(s1map, s2map))
return true;
}
return false;
}
public boolean matches(int[] s1map, int[] s2map) {
for (int i = 0; i < 26; i++) {
if (s1map[i] != s2map[i])
return false;
}
return true;
}
// V5
// IDEA : Sliding Window
// https://leetcode.com/problems/permutation-in-string/editorial/
public boolean checkInclusion_6(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_6(s1map, s2map);
}
public boolean matches_6(int[] s1map, int[] s2map) {
for (int i = 0; i < 26; i++) {
if (s1map[i] != s2map[i])
return false;
}
return true;
}
// V6
// IDEA : OPTIMIZED SLIDING WINDOW
// https://leetcode.com/problems/permutation-in-string/editorial/
public boolean checkInclusion_7(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']++;
}
int count = 0;
for (int i = 0; i < 26; i++) {
if (s1map[i] == s2map[i])
count++;
}
for (int i = 0; i < s2.length() - s1.length(); i++) {
int r = s2.charAt(i + s1.length()) - 'a', l = s2.charAt(i) - 'a';
if (count == 26)
return true;
s2map[r]++;
if (s2map[r] == s1map[r]) {
count++;
} else if (s2map[r] == s1map[r] + 1) {
count--;
}
s2map[l]--;
if (s2map[l] == s1map[l]) {
count++;
} else if (s2map[l] == s1map[l] - 1) {
count--;
}
}
return count == 26;
}
// V5
// https://github.com/neetcode-gh/leetcode/blob/main/java/0567-permutation-in-string.java
public boolean checkInclusion_8(String s1, String s2) {
int n = s1.length();
int[] freq = new int[26];
int m = s2.length();
for (int i = 0; i < n; i++) {
freq[s1.charAt(i) - 'a']++;
}
int[] freq2 = new int[26];
for (int i = 0; i < m; i++) {
freq2[s2.charAt(i) - 'a']++;
if (i >= n) {
freq2[s2.charAt(i - n) - 'a']--;
}
if (Arrays.equals(freq, freq2))
return true;
}
return false;
}
}