3 ms/87% Easy Java solution


  • 0
    W
    public class Solution {
        String lower = "zxcvbnm";
        String middle ="asdfghjkl";
        String upper ="qwertyuiop";
        public String[] findWords(String[] words) {
            if(words == null) return new String[0];
            List<String> res = new ArrayList<String>();
             for(String word: words){
                 if(helper(word))
                   res.add(word);
             }
             return res.toArray(new String[res.size()]);
        }
        
        private boolean helper(String s){
            s = s.toLowerCase();
            char first = s.charAt(0);
            for(int i = 1; i < s.length(); i++){
                char c = s.charAt(i);
                if(lower.indexOf(first) != -1){
                    if(lower.indexOf(c) == -1) 
                    return false;
                }else if(middle.indexOf(first) != -1){
                    if(middle.indexOf(c) == -1) 
                    return false;
                }else{
                    if(upper.indexOf(c) == -1) 
                    return false;
                }
          
            }
            
            return true;
        }
    }
    

Log in to reply
 

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