567. Permutation in String
202407132009
tags: #sliding-window
var checkInclusion = function(s1, s2) {
const map = new Map();
for (const c of s1) {
map.set(c, (map.get(c) ?? 0) + 1);
}
let start = 0;
let count = 0;
for (let end = 0; end < s2.length; end++) {
map.set(s2[end], (map.get(s2[end]) ?? 0) - 1);
if (map.get(s2[end]) >= 0) {
count += 1;
}
if (end - start + 1 > s1.length) {
map.set(s2[start], map.get(s2[start]) + 1);
if (map.get(s2[start]) > 0) {
count -= 1;
}
start += 1;
}
if (count === s1.length) {
return true;
}
}
return false;
};