-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
29 lines (26 loc) · 743 Bytes
/
solution.js
File metadata and controls
29 lines (26 loc) · 743 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
/**
* @param {string[]} words
* @param {string} pattern
* @return {string[]}
*/
var findAndReplacePattern = function(words, pattern) {
let res = [],
length = pattern.length
words.forEach(word => {
let charMap = {},
charReverseMap = {},
notMatch = false
for (let i = 0; i < length; i++) {
if (!charMap[word[i]] && !charReverseMap[pattern[i]]) {
charMap[word[i]] = pattern[i]
charReverseMap[pattern[i]] = word[i]
} else if (charMap[word[i]] != pattern[i]) {
notMatch = true
break
}
}
if (!notMatch)
res.push(word)
})
return res
};