-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_500.java
More file actions
37 lines (32 loc) · 1.15 KB
/
Copy pathP_500.java
File metadata and controls
37 lines (32 loc) · 1.15 KB
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
36
37
package leetcode.easy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class P_500 {
public static final String[] STRINGS = {};
public String[] findWords(String[] words) {
final Set<Character> row1 = new HashSet<>(
Arrays.asList('q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'));
final Set<Character> row2 = new HashSet<>(
Arrays.asList('a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'));
final Set<Character> row3 = new HashSet<>(
Arrays.asList('z', 'x', 'c', 'v', 'b', 'n', 'm'));
final List<String> res = new ArrayList<>();
for (String word : words) {
if (checkRow(row1, word) || checkRow(row2, word) || checkRow(row3, word)) {
res.add(word);
}
}
return res.toArray(STRINGS);
}
private static boolean checkRow(Set<Character> row, String word) {
for (char c : word.toCharArray()) {
if (!row.contains(Character.toLowerCase(c))) {
return false;
}
}
return true;
}
}