-
Notifications
You must be signed in to change notification settings - Fork 0
/
substring-with-concatenation-of-all-words.js
70 lines (51 loc) · 1.9 KB
/
substring-with-concatenation-of-all-words.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @param {string} s
* @param {string[]} words
* @return {number[]}
*/
var findSubstring = function(s, words) {
// Resultant list
const indices = [];
// Base conditions
if (s === null || s.length === 0 || words === null || words.length == 0) {
return indices;
}
// Store the words and their counts in a hash map
const wordCount = words.reduce((a, b) => {
a[b] = (a[b] + 1) || 1;
return a;
}, {});
// Length of each word in the words array
const wordLength = words[0].length;
// Length of all the words combined in the array
const wordArrayLength = wordLength * words.length;
// Loop for the entire string
for (let i = 0; i <= s.length - wordArrayLength; i++) {
// Get the substring of length equal to wordArrayLength
let current = s.substring(i, i + wordArrayLength);
// Map to store each word of the substring
const wordMap = {};
// Index to loop through the words array
let index = 0;
// Index to get each word in the current
let j = 0;
// Loop through each word of the words array
while (index < words.length) {
// Divide the current string into strings of length of
// each word in the array
const part = current.substring(j, j + wordLength);
// Put this string into the wordMap
wordMap[part] = (wordMap[part] + 1) || 1;
// Update j and index
j += wordLength;
index++;
}
// At this point compare the maps
let a = JSON.stringify(Object.entries(wordCount).sort());
let b = JSON.stringify(Object.entries(wordMap).sort());
if (a === b) {
indices.push(i);
}
}
return indices;
};