500. Keyboard Row
class Solution {
public static String[] findWords(String[] words) {
String[] strs = { "QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM" };
HashMap map = new HashMap();
for (int i = 0; i < strs.length; i++) {
for (char c : strs[i].toCharArray()) {
map.put(c, i);// put pair into the map
}
}
ArrayList outArrList = new ArrayList();
for (int j = 0; j < words.length; j++) {
HashSet row = new HashSet();
for (int i = 0; i < words[j].length(); i++) {
Character current = words[j].charAt(i);
row.add(map.get(Character.toUpperCase(current)));
}
if (row.size() == 1)
outArrList.add(words[j]);
}
String[] out = new String[outArrList.size()];
for (int i = 0; i < out.length; i++)
out[i] = outArrList.get(i);
return out;
}
}
Like this:
Like Loading...
Related