-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0890-find-and-replace-pattern.js
More file actions
33 lines (29 loc) · 1008 Bytes
/
0890-find-and-replace-pattern.js
File metadata and controls
33 lines (29 loc) · 1008 Bytes
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
/**
* Find And Replace Pattern
* Time Complexity: O(N * L)
* Space Complexity: O(N * L)
*/
var findAndReplacePattern = function (wordsInput, patternInput) {
function createNormalizedCode(targetString) {
let charToIntegerMap = {};
let nextAvailableInteger = 0;
let resultantIntegerSequence = [];
for (let charIdentifier of targetString) {
if (charToIntegerMap[charIdentifier] === undefined) {
charToIntegerMap[charIdentifier] = nextAvailableInteger;
nextAvailableInteger++;
}
resultantIntegerSequence.push(charToIntegerMap[charIdentifier]);
}
return resultantIntegerSequence.join(",");
}
let patternCanonicalForm = createNormalizedCode(patternInput);
let matchedWordsCollection = [];
for (let currentWord of wordsInput) {
let wordCanonicalForm = createNormalizedCode(currentWord);
if (wordCanonicalForm === patternCanonicalForm) {
matchedWordsCollection.push(currentWord);
}
}
return matchedWordsCollection;
};