-
Notifications
You must be signed in to change notification settings - Fork 819
/
KeyboardRow.java
55 lines (52 loc) · 1.3 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
45
46
47
48
49
50
51
52
53
54
55
package string;
/**
* Created by gouthamvidyapradhan on 09/04/2019
*
* <p>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.
*
* <p>Example:
*
* <p>Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["Alaska", "Dad"]
*
* <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.
*/
import java.util.*;
public class KeyboardRow {
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {}
public String[] findWords(String[] words) {
String R1 = "qwertyuiop";
String R2 = "asdfghjkl";
String R3 = "zxcvbnm";
List<String> answer = new ArrayList<>();
for (String s : words) {
Set<Character> set = new HashSet<>();
for (char c : s.toCharArray()) {
if (R1.indexOf(c) != -1) {
set.add('1');
} else if (R2.indexOf(c) != -1) {
set.add('2');
} else if (R3.indexOf(c) != -1) {
set.add('3');
}
}
if (set.size() == 1) {
answer.add(s);
}
}
String[] ans = new String[answer.size()];
int i = 0;
for (String s : answer) {
ans[i++] = s;
}
return ans;
}
}