Given an array of strings words
, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.
In the American keyboard:
the first row consists of the characters "qwertyuiop"
,
the second row consists of the characters "asdfghjkl"
, and
the third row consists of the characters "zxcvbnm"
.
forEach
를 사용하지 않았다면 tmp
를 스택으로 활용해서 [-1]
과 비교해서 해결하는 것도 좋을 것 같다.var findWords = function(words) {
var first = "qwertyuiop";
var second = "asdfghjkl";
var last = "zxcvbnm";
var res = new Array();
words.forEach((word) => {
var tmp = new Set();
word.toLowerCase().split('').forEach((e) => {
if(first.includes(e)){
tmp.add(1);
} else if(second.includes(e)){
tmp.add(2);
} else{
tmp.add(3);
}
});
if(tmp.size === 1){
res.push(word);
}
});
return res;
};