-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
64 lines (59 loc) · 1.88 KB
/
Solution.java
File metadata and controls
64 lines (59 loc) · 1.88 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package keyboardrow;
import java.util.ArrayList;
import java.util.List;
/**
* Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
* Example:
* <p>
* Input: ["Hello", "Alaska", "Dad", "Peace"]
* Output: ["Alaska", "Dad"]
* <p>
* <p>
* Note:
* <p>
* You may use one character in the keyboard more than once.
* You may assume the input string will only contain letters of alphabet.
*/
class Solution {
char[][] rows = {{'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'},
{'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'},
{'Z', 'X', 'C', 'V', 'B', 'N', 'M'}};
String[] findWords(String[] words) {
boolean isMatch = true;
List<String> res = new ArrayList<String>();
for (String w : words) {
if (w.length() == 1) {
res.add(w);
continue;
}
int firstLetterRow = findRowOfLetter(w.charAt(0));
for (int i = 1; i < w.length(); i++) {
if (findRowOfLetter(w.charAt(i)) != firstLetterRow) {
isMatch = false;
break;
}
isMatch = true;
}
if (isMatch) {
res.add(w);
isMatch = false;
}
}
return res.toArray(new String[res.size()]);
}
private int findRowOfLetter(char c) {
for (int i = 0; i < rows.length; i++) {
for (char curr : rows[i]) {
if (Character.toLowerCase(curr) == Character.toLowerCase(c)) {
return i;
}
}
}
return -1;
}
}
/*
Details
Runtime: 0 ms, faster than 100.00% of Java online submissions for Keyboard Row.
Memory Usage: 37.7 MB, less than 7.69% of Java online submissions for Keyboard Row.
*/