Skip to content

Commit 6a6b6b3

Browse files
Added Permutations In String
1 parent 3633ace commit 6a6b6b3

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Permutation in String
3+
https://leetcode.com/problems/permutation-in-string/
4+
5+
Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.
6+
In other words, return true if one of s1's permutations is the substring of s2.
7+
8+
Example 1:
9+
10+
Input: s1 = "ab", s2 = "eidbaooo"
11+
Output: true
12+
Explanation: s2 contains one permutation of s1 ("ba").
13+
Example 2:
14+
15+
Input: s1 = "ab", s2 = "eidboaoo"
16+
Output: false
17+
18+
Constraints:
19+
1 <= s1.length, s2.length <= 104
20+
s1 and s2 consist of lowercase English letters.
21+
*/
22+
23+
24+
/**
25+
* @param {string} s1
26+
* @param {string} s2
27+
* @return {boolean}
28+
*/
29+
var checkInclusion = function(s1, s2) {
30+
if(s1.length > s2.length) {
31+
return false
32+
}
33+
34+
let start = 0;
35+
let end = s1.length - 1;
36+
let countLeft = s1.length;
37+
let hashBuild = {};
38+
39+
for(var i = 0; i < s1.length; i++) {
40+
hashBuild[s1[i]] = (hashBuild[s1[i]] || 0) + 1;
41+
}
42+
43+
for(var j = start; j < end; j++) { // TODO: didn't count upper bound
44+
if(hashBuild[s2[j]] !== undefined) {
45+
hashBuild[s2[j]] = hashBuild[s2[j]] - 1;
46+
if(hashBuild[s2[j]] >= 0) {
47+
countLeft--;
48+
}
49+
}
50+
}
51+
52+
while(end < s2.length) {
53+
if(hashBuild[s2[end]] !== undefined) {
54+
hashBuild[s2[end]] = hashBuild[s2[end]] - 1;
55+
if(hashBuild[s2[end]] >= 0) {
56+
countLeft--;
57+
}
58+
}
59+
60+
if(countLeft == 0) { return true }
61+
62+
if(hashBuild[s2[start]] !== undefined) {
63+
hashBuild[s2[start]] = hashBuild[s2[start]] + 1;
64+
if(hashBuild[s2[start]] >= 1) {
65+
countLeft++;
66+
}
67+
}
68+
69+
start++;
70+
end++;
71+
}
72+
73+
return false;
74+
};
75+
76+
module.exports.checkInclusion = checkInclusion;

LeetcodeProblems/RESOURCES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
[Common Patters on Leetcode problems]https://iorilan.medium.com/after-solved-1000-medium-leetcode-found-these-patterns-sliding-window-2-pointer-string-18332ca4861
1+
[Common Patters on Leetcode problems](https://iorilan.medium.com/after-solved-1000-medium-leetcode-found-these-patterns-sliding-window-2-pointer-string-18332ca4861)
22

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const assert = require('assert');
2+
const checkInclusion = require('../../LeetcodeProblems/Algorithms/Permutations_In_String').checkInclusion;
3+
4+
function test() {
5+
assert.equal(false, checkInclusion("abc", "ab"));
6+
assert.equal(false, checkInclusion("aa", "aba"));
7+
assert.equal(true, checkInclusion("ab", "eidbaooo"));
8+
assert.equal(false, checkInclusion("ab", "eidboaoo"));
9+
}
10+
test();
11+
12+
module.exports.test = test;

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
3939
| [Longest Palindromic Substring ](/LeetcodeProblems/Algorithms/Longest_Palindromic_Substring.js) | Medium | https://leetcode.com/problems/longest-palindromic-substring/|
4040
| [Longest Substring Without Reapeating Characters](/LeetcodeProblems/Algorithms/Longest_Substring.js) | Medium | https://leetcode.com/problems/longest-substring-without-repeating-characters|
4141
| [Max Area Of Island ](/LeetcodeProblems/Algorithms/Max_Area_Of_Island.js) | Medium | https://leetcode.com/problems/max-area-of-island/ |
42-
| [Max Consecutive Ones III ](/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js) | Medium | https://leetcode.com/problems/max-consecutive-ones-iii |
42+
| [Max Consecutive Ones III ](/LeetcodeProblems/Algorithms/Max_Consecutive_Ones_III.js) | Medium | https://leetcode.com/problems/max-consecutive-ones-iii |
4343
| [Maximal Square ](/LeetcodeProblems/Algorithms/Maximal_Square.js) | Medium | https://leetcode.com/problems/maximal-square/ |
4444
| [Minimum Size Subarray](/LeetcodeProblems/Algorithms/Minimum_Size_Subarray.js) | Medium | https://leetcode.com/problems/minimum-size-subarray-sum |
4545
| [Permutations ](/LeetcodeProblems/Algorithms/Permutations.js) | Medium | https://leetcode.com/problems/permutations/ |
4646
| [Permutations II ](/LeetcodeProblems/Algorithms/Permutations_II.js) | Medium | https://leetcode.com/problems/permutations-ii/ |
47+
| [Permutation in String](/LeetcodeProblems/Algorithms/Permutations_In_String.js) | Medium | https://leetcode.com/problems/permutation-in-string/ |
4748
| [Permutations Without Duplicates ](/LeetcodeProblems/Algorithms/Permutations_Without_Duplicates.js) | Medium | https://leetcode.com/problems/permutations/ |
4849
| [Restore IP Addresses ](/LeetcodeProblems/Algorithms/Restore_IP_Addresses.js) | Medium | https://leetcode.com/problems/restore-ip-addresses/ |
4950
| [SearchIng Rotated Sorted Array ](/LeetcodeProblems/Algorithms/SearchIng_Rotated_Sorted_Array.js) | Medium | https://leetcode.com/problems/search-in-rotated-sorted-array/ |

0 commit comments

Comments
 (0)