-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path966-Vowel-Spellchecker.js
43 lines (34 loc) · 1.19 KB
/
966-Vowel-Spellchecker.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
/**
* @param {string[]} wordList
* @param {string[]} queries
* @return {string[]}
*/
const spellchecker = (wordList, queries) => {
const set = new Set();
const map1 = new Map();
const map2 = new Map();
const removeVowels = (str) => {
let replaced = '';
str.split('').forEach((char) => {
replaced += /[aeiou]/gi.test(char) ? '*' : char;
});
return replaced;
};
wordList.forEach((word) => {
const wornInLowerCase = word.toLowerCase();
const wordWithoutVowels = removeVowels(wornInLowerCase);
set.add(word);
if (!map1.has(wornInLowerCase)) map1.set(wornInLowerCase, word);
if (!map2.has(wordWithoutVowels)) map2.set(wordWithoutVowels, word);
});
const result = [];
queries.forEach((query) => {
const wornInLowerCase = query.toLowerCase();
const wordWithoutVowels = removeVowels(wornInLowerCase);
if (set.has(query)) result.push(query);
else if (map1.has(wornInLowerCase)) result.push(map1.get(wornInLowerCase));
else if (map2.has(wordWithoutVowels)) result.push(map2.get(wordWithoutVowels));
else result.push('');
});
return result;
};