题目:
Given a List of words, return the words that can be typed using letters ofalphabeton only one row's of American keyboard like the image below.
代码:
/**
* @param {string[]} words
* @return {string[]}
*/
var findWords = function(words) {
let rows = {'Q':1,'q':1,'W':1,'w':1,'E':1,'e':1,'R':1,'r':1,'T':1,'t':1,'Y':1,'y':1,'U':1,'u':1,'I':1,'i':1,'O':1,'o':1,'P':1,'p':1,'A':2,'a':2,'S':2,'s':2,'D':2,'d':2,'F':2,'f':2,'G':2,'g':2,'H':2,'h':2,'J':2,'j':2,'K':2,'k':2,'L':2,'l':2,'Z':4,'z':4,'X':4,'x':4,'C':4,'c':4,'V':4,'v':4,'B':4,'b':4,'N':4,'n':4,'M':4,'m':4},ans=[];
for(let i=0;i<words.length;++i){
let word = words[i];
let flag = rows[word[0]];
for(let j=1;j<words.length;++j){
flag&=rows[word[j]];
}
if(flag!==0) {
ans.push(word);
}
}
return ans;
};