Java Solution


  • 0
    G
    class Solution {
        public String[] findWords(String[] words) {
            String[] row = new String[3];
             row[0] = "qwertyuiop"; row[0]=row[0] + row[0].toUpperCase();
             row[1] = "asdfghjkl"; row[1]=row[1] + row[1].toUpperCase();
             row[2] = "zxcvbnm"; row[2]=row[2] + row[2].toUpperCase();
            
            ArrayList<String> result = new ArrayList<>();
            
            for(int i=0;i<words.length;i++) {
                int rownum = 0;
                for(int j=0;j<row.length;j++) {
                    if ( row[j].indexOf(words[i].charAt(0)) != -1 )
                        rownum = j;
                }
                
                boolean canType = true;
                for(int k=1;k<words[i].length();k++) {
                    if ( row[rownum].indexOf( words[i].charAt(k) ) == -1 )
                        { canType = false; break; }               
                }
                if(canType == true) {
                    result.add(words[i]);
                }
            }
            
            return result.toArray(new String[result.size()]);
        }
    }
    

Log in to reply
 

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