Java Solution


  • 0
    K

    class Solution {
    public String[] findWords(String[] words) {
    int[] letters = new int[10000];
    letters['q'] = letters['w'] = letters['e'] = letters['r'] = letters['t'] = letters['y'] = letters['u'] = letters['i'] = letters['o'] = letters['p'] = 1;
    letters['a'] = letters['s'] = letters['d'] = letters['f'] = letters['g'] = letters['h'] = letters['j'] = letters['k'] = letters['l'] = 2;
    letters['z'] = letters['x'] = letters['c'] = letters['v'] = letters['b'] = letters['n'] = letters['m'] = 3;
    String[] result = new String[10000];
    int pos = 0;
    for(int i = 0 ; i < words.length ; i++)
    {
    boolean flag = false;
    for(int j = 1 ; j < words[i].length() ; j++){
    if(letters[words[i].toLowerCase().charAt(j)] != letters[words[i].toLowerCase().charAt(j-1)]){
    flag = true;
    break;
    }
    }
    if(!flag)
    result[pos++] = words[i];
    }
    String[] res = new String[pos];
    for(int k = 0 ; k < pos ; k++)
    res[k] = result[k];
    return res;
    }
    }


Log in to reply
 

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