Skip to content

Commit 6fa456c

Browse files
author
baochau.dinh
committed
js-concepts: leetcode prob.567 - permutation string
1 parent 8ff2c80 commit 6fa456c

File tree

1 file changed

+29
-0
lines changed
  • LeetCode/567. Permutation in String

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var isPermutation = function(s1, s2) {
2+
if (s1.length !== s2.length) return false;
3+
4+
let hashMapOfS1 = {}, hashMapOfS2 = {};
5+
for (let i = 0; i < s1.length; i++) {
6+
hashMapOfS1[s1[i]] = hashMapOfS1[s1[i]] + 1 || 1;
7+
}
8+
for (let i = 0; i < s2.length; i++) {
9+
hashMapOfS2[s2[i]] = hashMapOfS2[s2[i]] + 1 || 1;
10+
}
11+
12+
for (keys in hashMapOfS1) {
13+
if (hashMapOfS1[keys] !== hashMapOfS2[keys]) return false;
14+
}
15+
16+
return true;
17+
}
18+
19+
var checkInclusion = function(s1, s2) {
20+
let permutationLength = s1.length;
21+
let first = 0, second = permutationLength - 1;
22+
while (first < s2.length && second < s2.length) {
23+
let currentStr = s2.slice(first, second + 1);
24+
if (isPermutation(s1, currentStr)) return true;
25+
first++;
26+
second++;
27+
}
28+
return false;
29+
};

0 commit comments

Comments
 (0)