-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
32 lines (32 loc) · 1 KB
/
solution.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
/**
* @param {string[]} words
* @param {string} pattern
* @return {string[]}
*/
var findAndReplacePattern = function (words, pattern) {
const result = [];
const Length = pattern.length;
for (let i = 0; i < words.length; i++) {
const word = words[i];
const word2pattern = {};
const pattern2word = {};
let flag = true;
for (let j = 0; j < Length; j++) {
const wordLetter = word[j];
const patternLettern = pattern[j];
if (word2pattern[wordLetter] === undefined) {
if (pattern2word[patternLettern] !== undefined) {
flag = false;
break;
}
word2pattern[wordLetter] = patternLettern;
pattern2word[patternLettern] = wordLetter;
} else if (word2pattern[wordLetter] !== patternLettern) {
flag = false;
break;
}
}
flag && result.push(word);
}
return result;
};