Skip to content

Commit ef9849d

Browse files
committed
[leet] 567. Permutation in String [medium]
1 parent 2131464 commit ef9849d

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

허현빈/5주차/260126-1.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
var permute = function(nums) {
6+
const ans =[]
7+
const bkArr =[ ]
8+
const visit = Array.from({length: nums.length}).fill(false)
9+
const bk = (k) =>{
10+
if(k >= nums.length){
11+
const newArr = JSON.parse(JSON.stringify(bkArr))
12+
ans.push(newArr)
13+
return
14+
}
15+
for(let i = 0 ; i < nums.length; i++){
16+
if(!visit[i]){
17+
visit[i] = true;
18+
bkArr[k] = nums[i]
19+
bk(k+1)
20+
visit[i] = false;
21+
}
22+
}
23+
}
24+
bk(0)
25+
return ans
26+
};

허현빈/5주차/260126-2.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {string} s1
3+
* @param {string} s2
4+
* @return {boolean}
5+
*/
6+
var checkInclusion = function(s1, s2) {
7+
8+
const s1Count = Array(26).fill(0);
9+
const s2Count = Array(26).fill(0);
10+
const aCode = 'a'.charCodeAt(0);
11+
12+
for (let i = 0; i < s1.length; i++) {
13+
s1Count[s1.charCodeAt(i) - aCode]++;
14+
s2Count[s2.charCodeAt(i) - aCode]++;
15+
}
16+
17+
const matches = (arr1, arr2) => {
18+
for (let i = 0; i < 26; i++) {
19+
if (arr1[i] !== arr2[i]) return false;
20+
}
21+
return true;
22+
};
23+
for (let i = 0; i < s2.length ; i++) {
24+
if (matches(s1Count, s2Count)) {
25+
return true;
26+
}
27+
s2Count[s2.charCodeAt(i + s1.length) - aCode]++;
28+
s2Count[s2.charCodeAt(i) - aCode]--;
29+
}
30+
31+
return matches(s1Count, s2Count);
32+
};

0 commit comments

Comments
 (0)