Java 2ms Simple Solution


  • 0
    G
    //row number for alphabets a to z
        private static int[] row = {1,2,2,1,3,1,1,1,3,1,1,1,2,2,3,3,3,3,1,3,3,2,3,2,3,2};
        
        public String[] findWords(String[] words) {
            List<String> result = new ArrayList<String>();
            
            for(String word : words) {
                char[] arr = word.toLowerCase().toCharArray();
                boolean sameRow = true;
                int assignedRow = row[arr[0] - 'a'];
                for(int i=1;i<arr.length;i++) {
                    if(row[arr[i] - 'a'] != assignedRow) {
                        sameRow = false;
                        break;
                    }
                }
                if(sameRow) result.add(word);
            }
            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.