-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpermutation-in-string.js
More file actions
56 lines (51 loc) · 1.9 KB
/
Copy pathpermutation-in-string.js
File metadata and controls
56 lines (51 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// https://leetcode.com/problems/permutation-in-string/
// Related Topics: Two Pointers, Sliding Window, Hash Table
// Difficulty: Medium
/*
Initial thoughts:
Creating a frequency table for the characters in s1 we are going to
have a changing frequency table for s2 that encompasses chracters equal
to the length of s1. Moving forward with the freq table on s2, we are going
to compare the two freq tables at each step. If they are identical, we have a
permutation of s1 in s2.
Since we are dealing with a predefined set of characters (in this case English
small letters) the comparision of the freq tables takes constant time (26 at most)
Creating the freq tables also won't take more than linear time equal to the length
of s2.
Time complexity: O(n) where n == len(s2)
Space complexity: O(1) because the freq tables won't have more than 26 chars
*/
/**
* @param {string} s1
* @param {string} s2
* @return {boolean}
*/
const checkInclusion = (s1, s2) => {
const dic1 = createFreqTable(s1);
const dic2 = createFreqTable(s2.slice(0, s1.length));
if (compareDics(dic1, dic2)) return true;
for (let i = s1.length; i < s2.length; i++) {
dic2.set(s2[i - s1.length], dic2.get(s2[i - s1.length]) - 1);
if (dic2.has(s2[i])) dic2.set(s2[i], dic2.get(s2[i]) + 1);
else dic2.set(s2[i], 1);
if (dic2.get(s2[i - s1.length]) === 0) dic2.delete(s2[i - s1.length]);
if (compareDics(dic1, dic2)) return true;
}
return false;
function compareDics(dic1, dic2) {
if (dic1.size !== dic2.size) return false;
for (const k of dic1.keys()) {
if (dic2.has(k) && dic1.get(k) === dic2.get(k)) continue;
return false;
}
return true;
}
function createFreqTable(s) {
m = new Map();
for (let c of s) {
if (m.has(c)) m.set(c, m.get(c) + 1);
else m.set(c, 1);
}
return m;
}
};