-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyBoardRow.java
More file actions
31 lines (24 loc) · 918 Bytes
/
KeyBoardRow.java
File metadata and controls
31 lines (24 loc) · 918 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
// 500. Keyboard Row
import java.util.*;
class Solution {
public String[] findWords(String[] words) {
Map<Character, Integer> map = new HashMap<>();
for (char c : "qwertyuiop".toCharArray()) map.put(c, 1);
for (char c : "asdfghjkl".toCharArray()) map.put(c, 2);
for (char c : "zxcvbnm".toCharArray()) map.put(c, 3);
List<String> result = new ArrayList<>();
for (String word : words) {
String lower = word.toLowerCase();
int row = map.get(lower.charAt(0));
boolean valid = true;
for (char c : lower.toCharArray()) {
if (map.get(c) != row) {
valid = false;
break;
}
}
if (valid) result.add(word);
}
return result.toArray(new String[0]);
}
}