Skip to content

Commit f48aa2f

Browse files
authored
Update 567-permutation-in-string.js
1 parent cbb9853 commit f48aa2f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

567-permutation-in-string.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,36 @@ function matches(s1map, s2map) {
3131
}
3232
return true
3333
}
34+
35+
36+
// another
37+
38+
/**
39+
* @param {string} s1
40+
* @param {string} s2
41+
* @return {boolean}
42+
*/
43+
const checkInclusion = function(s1, s2) {
44+
const arr = Array(26).fill(0)
45+
const a = 'a'.charCodeAt(0)
46+
const s1l = s1.length
47+
for(let c of s1) arr[c.charCodeAt(0) - a] += 1
48+
for(let i = 0, len = s2.length; i < len; i++) {
49+
const tmp = s2[i]
50+
arr[tmp.charCodeAt(0) - a]--
51+
if(i >= s1l - 1) {
52+
if(allZeros(arr)) return true
53+
arr[s2.charCodeAt(i - s1l + 1) - a]++
54+
}
55+
56+
}
57+
58+
return false
59+
};
60+
61+
function allZeros(arr) {
62+
for(let e of arr) {
63+
if(e !== 0) return false
64+
}
65+
return true
66+
}

0 commit comments

Comments
 (0)