Repository navigation
Expand file tree
/
Copy pathKeyboardRow.java
More file actions
63 lines (59 loc) · 1.48 KB
/
Copy pathKeyboardRow.java
File metadata and controls
63 lines (59 loc) · 1.48 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
package string; /**
* Created by gouthamvidyapradhan on 09/04/2019
*
* 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:
*
* Input: ["Hello", "Alaska", "Dad", "Peace"]
* Output: ["Alaska", "Dad"]
*
* Note:
*
* 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;
}
}