short java solution and easy understand


  • 0
    C
    public class Solution {
        public String[] findWords(String[] words) {
                            //a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z
            int[] charRows = {2, 1, 1, 2, 3, 2, 2, 2, 3, 2, 2, 2, 1, 1, 3, 3, 3, 3, 2, 3, 3, 1, 3, 1, 3, 1};
            
            ArrayList<String> result = new ArrayList<>();
            
            for (String word : words) {
                char[] chars = word.toLowerCase().toCharArray();
                int pos = 0;
                int len = chars.length - 1;
                for (; pos < len; pos ++) {
                    if (charRows[chars[pos] - 'a'] != charRows[chars[pos+1] - 'a']) {
                        break;
                    }
                }
                if (pos == len) {
                    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.