Easy understand java code using HashMap


  • 0
    Z

    String[] strs = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
    Map<Character, Integer> map = new HashMap<Character, Integer>();
    for (int i = 0; i < 3; i++) {
    char[] tmp = strs[i].toCharArray();
    for (int j = 0; j < tmp.length; j++) {
    map.put(tmp[j], i);
    }
    }
    List<String> result = new ArrayList<String>();
    outer:
    for (String word : words) {
    if (word == "") {
    continue outer;
    }
    String lowCaseWord = word.toLowerCase();
    char[] tmp = lowCaseWord.toCharArray();
    int index = map.get(tmp[0]);
    for (int m = 1; m < tmp.length; m++) {
    if (map.get(tmp[m]) != index) {
    continue outer;
    }
    }
    result.add(word);
    }
    return result.toArray(new String[result.size()]);


Log in to reply
 

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