Skip to content

Commit 8bf3d6c

Browse files
committed
Add JS permutation in string LC medium
1 parent 442768a commit 8bf3d6c

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/leetcode/medium/medium.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- [Number of Islands](number-of-islands)
5959
- [Pacific Atlantic Water Flow](pacific-atlantic-water-flow)
6060
- [Palindromic Substring](palindromic-substring)
61+
- [Permutation in String](permutation-in-string)
6162
- [Product of Array Except Self](product-of-array-except-self)
6263
- [Remove Nth Node From End of List](remove-nth-node-from-end-of-list)
6364
- [Reorder List](reorder-list)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
https://leetcode.com/problems/permutation-in-string/description
3+
4+
You are given two strings s1 and s2.
5+
6+
Return true if s2 contains a permutation of s1, or false otherwise. That means if a permutation of s1 exists as a substring of s2, then return true.
7+
8+
Both strings only contain lowercase letters.
9+
10+
Example 1:
11+
Input: s1 = "abc", s2 = "lecabee"
12+
Output: true
13+
Explanation: The substring "cab" is a permutation of "abc" and is present in "lecabee".
14+
15+
Example 2:
16+
Input: s1 = "abc", s2 = "lecaabee"
17+
Output: false
18+
19+
Constraints:
20+
1 <= s1.length, s2.length <= 1000
21+
*/
22+
23+
const checkInclusion = (s1, s2) => {
24+
const s1Chars = {};
25+
for (const char of s1) {
26+
s1Chars[char] = s1Chars[char] ?? 0;
27+
s1Chars[char] += 1;
28+
}
29+
30+
let hasPermutation = false;
31+
let l = 0;
32+
let r = 0;
33+
34+
while (r < s2.length) {
35+
const s2Char = s2[r];
36+
if (s1Chars[s2Char]) {
37+
s1Chars[s2Char] -= 1;
38+
39+
if (s1Chars[s2Char] === 0) {
40+
delete s1Chars[s2Char];
41+
}
42+
43+
if (Object.keys(s1Chars).length === 0) {
44+
hasPermutation = true;
45+
break;
46+
}
47+
48+
r += 1;
49+
} else {
50+
const replacementS2Char = s2[l];
51+
s1Chars[replacementS2Char] = s1Chars[replacementS2Char] ?? 0;
52+
s1Chars[replacementS2Char] += 1;
53+
l += 1;
54+
}
55+
}
56+
57+
return hasPermutation;
58+
};
59+
60+
module.exports = { checkInclusion };
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { checkInclusion } = require("./permutationInString");
2+
3+
describe("checkInclusion", () => {
4+
it("returns true if s2 contains a permutation of s1", () => {
5+
const result = checkInclusion("abc", "lecabee");
6+
expect(result).toBeTruthy();
7+
});
8+
9+
it("returns false if s2 does not contain a permutation of s1", () => {
10+
const result = checkInclusion("abc", "lecaabee");
11+
expect(result).toBeFalsy();
12+
});
13+
14+
it("correctly returns true if s2 contains a permutation of s1 with multiple characters", () => {
15+
const result = checkInclusion("adc", "dadclecabee");
16+
expect(result).toBeTruthy();
17+
});
18+
});

0 commit comments

Comments
 (0)