首页 > 代码库 > LeetCode-500. Keyboard Row

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

public class Solution {
    public String[] findWords(String[] words) {
        if (words == null)
            return new String[0];
        String[] r = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
        List<String> rets = new ArrayList<String>();
        for (String ws : words) {
            String w = ws.toLowerCase();
            char first = w.charAt(0);
            int in = 0;
            while (in < 3) {
                if (r[in].contains(first+""))
                    break;
                in ++;
            }
            boolean all = true;
            for (int i=1; i<w.length(); i++) {
                if (!r[in].contains(w.charAt(i)+"")) {
                    all = false;
                    break;
                }
            }
            if (all) {
                rets.add(ws);
            }
        }
        return rets.toArray(new String[0]);
    }
    
   
}

 

LeetCode-500. Keyboard Row