You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* I came up with below solution independently on 9/17/2021.
51
+
* <p>
52
+
* A few pointers that led me to the sliding window approach:
53
+
* 1. if it's a valid permutation, the substring from S2 must have equal length as of s1;
54
+
* 2. I don't want to repeatedly calculate each and every possible substring of s2, if s1 is really long, this could mean lots of redundant calculation.
55
+
* So sliding window to the rescue!
56
+
*/
57
+
publicbooleancheckInclusion(Strings1, Strings2) {
58
+
if (s1.length() > s2.length()) {
59
+
returnfalse;
60
+
}
61
+
int[] count = newint[26];
62
+
for (charc : s1.toCharArray()) {
63
+
count[c - 'a']++;
64
+
}
65
+
for (inti = 0; i < s1.length(); i++) {
66
+
count[s2.charAt(i) - 'a']--;
67
+
}
68
+
for (inti = s1.length(), j = 0; i < s2.length(); i++, j++) {
0 commit comments