【LeetCode】500. Keyboard Row


Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.


American keyboard


Example 1:

Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]

Note:

  1. You may use one character in the keyboard more than once.
  2. You may assume the input string will only contain letters of alphabet.

很简单的一个题目,判断给定单词是不是能在键盘上用一行打出来。



var s1='qwertyuiopQWERTYUIOP',s2='asdfghjklASDFGHJKL',s3='zxxcvbnmZXCVBNM';

var findWords = function(words) {
    var ret=[];
    for(var i=0;i<words.length;i++){
        var flag=true;
        if(s1.indexOf(words[i][0])>=0){
            for(var j=0;j<words[i].length;j++){
               if(s1.indexOf(words[i][j])<0){
                   flag=false;
               }        
            }
        }else if(s2.indexOf(words[i][0])>=0){
             for(var j=0;j<words[i].length;j++){
               if(s2.indexOf(words[i][j])<0){
                   flag=false;
               }        
            }    
        }else if(s3.indexOf(words[i][0])>=0){
             for(var j=0;j<words[i].length;j++){
               if(s3.indexOf(words[i][j])<0){
                   flag=false;
               }        
            }    
        }
        if(flag){
            ret.push(words[i]);
        }
    }
    return ret;
};








猜你喜欢

转载自blog.csdn.net/lx583274568/article/details/75581841