java solution


  • 0
    I

    public String[] findWords(String[] words) {
    ArrayList<String> list = new ArrayList();
    String arr1[] = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p"};
    String arr2[] = {"a", "s", "d", "f", "g", "h", "j", "k", "l"};
    String arr3[] = {"z", "x", "c", "v", "b", "n", "m"};
    HashMap temp = new HashMap();
    for(String s:arr1)
    {
    temp.put(s, 1);
    }
    for(String s:arr2)
    {
    temp.put(s, 2);
    }
    for(String s:arr3)
    {
    temp.put(s, 3);
    }
    for(int i=0;i<words.length;i++)
    {
    int judge = 0;
    for(int j=0;j<words[i].length();j++)
    {
    if (temp.get("" + words[i].toLowerCase().charAt(0)) != temp.get("" + words[i].toLowerCase().charAt(j)))
    {
    judge = 1;
    break;
    }
    }
    if(judge != 1)
    {
    list.add(words[i]);
    }
    }
    String[] ans = new String[list.size()];
    return (String[]) list.toArray(ans);
    }


Log in to reply
 

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