Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e01c48e

Browse files
committed
day-18
1 parent 8292d4f commit e01c48e

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
- Day 13: [Remove K Digits](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-thirteen/index.ts) :tongue:
1717
- Day 14: [Implement Trie (Prefix Tree)](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-thirteen/index.ts) :pig_nose:
1818
- Day 15: [Maximum Sum Circular Subarray](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-fifteen/index.ts) :whale:
19-
- Day 16: [Odd Even Linked List](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-sixteen/index.ts) :whale:
20-
- Day 17: [Find All Anagrams in a String Solution](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-seventeen/index.ts) :whale:
19+
- Day 16: [Odd Even Linked List](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-sixteen/index.ts) :grin:
20+
- Day 17: [Find All Anagrams in a String Solution](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-seventeen/index.ts) :hear_no_evil:
21+
- Day 18: [Permutation in String](https://github.com/libterty/leetcode-challenge/blob/master/src/may/day-eighteen/index.ts) :pig_nose:

‎src/may/day-eighteen/index.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @param {string} s1
3+
* @param {string} s2
4+
* @return {boolean}
5+
*/
6+
7+
var checkInclusion = function (s1: string, s2: string): boolean {
8+
const len1: number = s1.length;
9+
const len2: number = s2.length;
10+
11+
if (len1 > len2) return false;
12+
let alphas = Array.from({ length: 26 }, (x) => 0);
13+
let begin = 'a'.charCodeAt(0);
14+
for (let a of s1) alphas[a.charCodeAt(0) - begin]++;
15+
16+
let len = s1.length;
17+
let count = Array.from({ length: 26 }, (x) => 0);
18+
//initialize count
19+
for (let i = 0; i < len; i++) {
20+
count[s2[i].charCodeAt(0) - begin]++;
21+
}
22+
23+
//sliding window
24+
for (let i = len; i <= s2.length; i++) {
25+
let fit = true;
26+
for (let i = 0; i < 26; i++) {
27+
if (count[i] !== alphas[i]) {
28+
fit = false;
29+
break;
30+
}
31+
}
32+
if (fit) return true;
33+
if (i === s2.length) return false;
34+
//update count
35+
count[s2[i - len].charCodeAt(0) - begin]--;
36+
count[s2[i].charCodeAt(0) - begin]++;
37+
}
38+
};
39+
40+
const cd = checkInclusion('ab', 'eidbaooo');
41+
const de = checkInclusion('abc', 'ccccbbbbaaaa');
42+
console.log('cd', cd);
43+
console.log('de', de);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /