【LeetCode】500. Keyboard Row

来源:互联网 发布:淘宝美工和室内设计师 编辑:程序博客网 时间:2024/06/05 09:35


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;};








原创粉丝点击