Share My Java 3ms Solution


  • 1
    K
    public class Solution {
        private int[] letterToRowNumMap = 
            {2, 3, 3, 2, 1, 2, 2, 2, 1, 2, 2, 2, 3, 3,
             1, 1, 1, 1, 2, 1, 1, 3, 1, 3, 1, 3}; 
             
        public String[] findWords(String[] words) {
            List<String> result = new ArrayList<>();
            outer:
            for(String word : words) {
                int rowNum = getRowNum(word.charAt(0));
                for(char letter : word.toCharArray()) {
                    if(rowNum != getRowNum(letter)) {
                        continue outer;
                    }
                }
                result.add(word);
            }
            return result.toArray(new String[result.size()]);
            
        }
        
        private int getRowNum(char letter) {
            return letterToRowNumMap[Character.toLowerCase(letter) - 'a'];
        }
    }
    

Log in to reply
 

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