-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path500-Keyboard_Row.js
More file actions
45 lines (35 loc) · 1 KB
/
500-Keyboard_Row.js
File metadata and controls
45 lines (35 loc) · 1 KB
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
// 57ms, 94.15%
// 42MB
const findWords = function(words) {
const result = [];
const first = "qwertyuiop";
const second = "asdfghjkl";
const third = "zxcvbnm";
words.forEach(word => {
let row = "";
const targetWord = word.toLowerCase();
switch(true) {
case first.indexOf(targetWord[0]) !== -1 :
row = first;
break;
case second.indexOf(targetWord[0]) !== -1 :
row = second;
break;
case third.indexOf(targetWord[0]) !== -1:
row = third;
break;
default:
return;
}
for(let i = 1; i < targetWord.length; i++) {
if (row.indexOf(targetWord[i]) === -1) {
return;
}
}
result.push(word);
})
return result;
};
console.log(findWords(["Hello","Alaska","Dad","Peace"]));
console.log(findWords(["omk"]));
console.log(findWords(["adsdf","sfd"]));