My java solution


  • 0
    M
    public class Solution {
        public String[] findWords(String[] words) {
            boolean check;
            List<String> result = new ArrayList<>();
            String[] keyboard = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
            Map<String, Integer> M = new HashMap<>();
            for (int i = 0; i < keyboard.length; i++) {
                for (String c : keyboard[i].split(""))
                    M.put(c, i);
            }
            for (int j = 0; j < words.length; j++) {
                check = true;
                String[] c = words[j].split("");
                for (int k = 0; k < c.length - 1; k++) {
                    if (M.get(c[k].toLowerCase()) != M.get(c[k + 1].toLowerCase())) {
                        check = false;
                        break;
                    }
                }
                if (check)
                    result.add(words[j]);
            }
            return result.toArray(new String[0]);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.