-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyboardRow.java
executable file
·44 lines (34 loc) · 1.18 KB
/
KeyboardRow.java
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
38
39
40
41
42
43
44
package hashTable;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by cuixiaodao on 11/03/2017
* for leetCode problem:
* https://leetcode.com/problems/keyboard-row/description/
*/
public class KeyboardRow {
private HashMap<Character, Integer> keyRowMap = new HashMap<Character, Integer>();
private void init() {
for (char c : "qwertyuiop".toCharArray()) keyRowMap.put(c, 1);
for (char c : "asdfghjkl".toCharArray()) keyRowMap.put(c, 2);
for (char c : "zxcvbnm".toCharArray()) keyRowMap.put(c, 3);
}
public String[] findWords(String[] words) {
init();
ArrayList<String> filteredWords = new ArrayList<String>();
for (String w : words) {
char[] chars = w.toLowerCase().toCharArray();
int r = keyRowMap.get(chars[0]);
boolean sameRow = true;
for (char c : chars)
if (keyRowMap.get(c) != r) {
sameRow = false;
break;
}
if (sameRow) filteredWords.add(w);
}
String[] ans = new String[filteredWords.size()];
filteredWords.toArray(ans);
return ans;
}
}