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.

500. Keyboard Row

 

Example 1:

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

 

My Solution:

public class Solution {
    public String[] findWords(String[] words) {
        String q = "[qwertyuiop]+";
        String a = "[asdfghjkl]+";
        String z = "[zxcvbnm]+";

        List<String> list = new ArrayList<String>();
        
        for(int i = 0; i < words.length; i++){
            if(words[i].toLowerCase().matches(q) || words[i].toLowerCase().matches(a) || words[i].toLowerCase().matches(z)){
                list.add(words[i]);
            }    
        }
        
        String[] arr = new String[list.size()];
        return list.toArray(arr);
    }
}

 

posted on 2017-02-21 13:35 LuoJunC 阅读(...) 评论(...) 编辑 收藏

相关文章:

  • 2021-08-12
  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
  • 2021-11-22
  • 2022-01-03
  • 2021-08-02
猜你喜欢
  • 2021-05-15
  • 2021-10-15
  • 2021-09-15
相关资源
相似解决方案