/**
 * Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.
 */

class Solution {
    public String[] findWords(String[] words) {
        String[] rows = new String[]{"qwertyuiop", "asdfghjkl", "zxcvbnm"};
        
        int currentWordRow = -1;
        boolean isWordValid = false;
        ArrayList<String> outputWords = new ArrayList<>();
        int curr = 0;
        
        for(String word : words){
            for(String character : word.split("")){
                
                for(int x = 0; x < rows.length; x++){
                    if(rows[x].contains(character)){
                        if(currentWordRow == -1){
                            currentWordRow = x;
                            isWordValid = true;
                        } else if (currentWordRow != x){
                            isWordValid = false;
                            break;
                        }
                    }
                }
            }
            if(isWordValid){
                outputWords.add(word);
            }
            currentWordRow = -1;
            isWordValid = false;
        } 
        
        String[] strArr = new String[outputWords.size()];  
        strArr = outputWords.toArray(strArr);
        return strArr; 
    }
}
@@ -1353,9 +1353,1185 @@
  %0A    %7D%0A
+%0A%0A    /**%0A     * Much faster and less space since it uses a hashtable for ascii%0A     */%0A    public String%5B%5D findWords(String%5B%5D words) %7B%0A        %0A        int%5B%5D charHash = new int%5B26%5D;%0A        for(char c : %22qwertyuiop%22.toCharArray())%7B charHash%5Bc - 'a'%5D = 1;%7D%0A        for(char c : %22asdfghjkl%22.toCharArray())%7B charHash%5Bc - 'a'%5D = 2;%7D%0A        for(char c : %22zxcvbnm%22.toCharArray())%7B charHash%5Bc - 'a'%5D = 3;%7D%0A        ArrayList%3CString%3E outputs = new ArrayList%3C%3E();%0A        %0A        for(String word : words)%7B%0A            int curr = -1;%0A            boolean canType = false;%0A            for(char c : word.toCharArray())%7B%0A                char h = Character.toLowerCase(c);%0A                if(curr == -1)%7B%0A                    curr = charHash%5Bh - 'a'%5D;%0A                    canType = true;%0A                %7D else if (curr != charHash%5Bh - 'a'%5D)%7B%0A                    canType = false;%0A                    break;%0A                %7D%0A            %7D%0A            if(canType) %7Boutputs.add(word);%7D%0A            curr = -1;%0A            canType = false;%0A        %7D%0A        %0A        String%5B%5D wordsArr = new String%5Boutputs.size()%5D;%0A        wordsArr = outputs.toArray(wordsArr);%0A        return wordsArr;%0A    %7D%0A%0A%0A
 %7D

update keyboard_row to use hashtables