forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboard Row.js
28 lines (25 loc) · 951 Bytes
/
Keyboard Row.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
var findWords = function(words) {
const firstRow = {"q":true,"w":true,"e":true,"r":true,"t":true,"y":true,"u":true,"i":true,"o":true,"p":true }
const secondRow = {"a":true,"s":true,"d":true,"f":true,"g":true,"h":true,"j":true,"k":true,"l":true }
const thirdRow = {"z":true,"x":true,"c":true,"v":true,"b":true,"n":true,"m":true }
let result = [];
const helperFunc= (word)=>{
let targetRow;
//Determine in which row the word should be;
if(firstRow[word[0].toLowerCase()]) targetRow = firstRow;
if(secondRow[word[0].toLowerCase()]) targetRow = secondRow;
if(thirdRow[word[0].toLowerCase()]) targetRow = thirdRow;
for(let i = 1;i<word.length;i++) {
if(!targetRow[word[i].toLowerCase()]) {
return false;
}
}
return true;
}
for(let i =0;i<words.length;i++) {
if(helperFunc(words[i])) {
result.push(words[i])
}
}
return result;
};