-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathKeyboard Row.java
More file actions
35 lines (34 loc) · 936 Bytes
/
Keyboard Row.java
File metadata and controls
35 lines (34 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public String[] findWords(String[] words) {
Map<Character, Integer> map = new HashMap<>();
update(map, "qwertyuiop", 1);
update(map, "asdfghjkl", 2);
update(map, "zxcvbnm", 3);
List<Integer> list = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
int rowIdx = -1;
for (char c : words[i].toCharArray()) {
if (rowIdx == -1) {
rowIdx = map.get(Character.toLowerCase(c));
}
if (map.get(Character.toLowerCase(c)) != rowIdx) {
rowIdx = -1;
break;
}
}
if (rowIdx != -1) {
list.add(i);
}
}
String[] ans = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
ans[i] = words[list.get(i)];
}
return ans;
}
private void update(Map<Character, Integer> map, String s, int row) {
for (char c : s.toCharArray()) {
map.put(c, row);
}
}
}