Java Solution


  • 0
    I
    public String[] findWords(String[] words) {
            String top = "qwertyuiopQWERTYUIOP";
            String mid = "asdfghjklASDFGHJKL";
            String low = "zxcvnbmZXCVBNM";
            
            Map<Character,Integer> map = new HashMap<Character,Integer>();
            
            for(int i=0;i<top.length();i++){
                map.put(top.charAt(i),1);
            }
            for(int i=0;i<mid.length();i++){
                map.put(mid.charAt(i),2);
            }
            for(int i=0;i<low.length();i++){
                map.put(low.charAt(i),3);
            }
            
            List<String> list = new ArrayList<String>();
            
            for(int i=0;i<words.length;i++){
                String word = words[i];
                char c = word.charAt(0);
                int val = map.get(new Character(c));
                int j=1;
                for(j=1;j<word.length();j++){
                    char d = word.charAt(j);
                    if(map.get(new Character(d))!=val){
                        break;
                    }
                }
                if(j==word.length()){
                    System.out.println(word);
                    list.add(word);
                }
                
            }
            String[] sol = list.toArray(new String[list.size()]);
            return sol;
        }
    

Log in to reply
 

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