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 1:
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.
程序如下所示:
class Solution {
public String[] findWords(String[] words) {
String[] s = new String[] {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
List<String> list = new ArrayList<>();
for (String str : words){
int len = str.length();
boolean[] a = new boolean[3];
for (int i = 0; i < len; ++ i){
if (s[0].contains(str.charAt(i) + "")){
a[0] = true;
if (a[1] || a[2]){
a[1] = a[0] = a[2] = false;
break;
}
continue;
}
if (s[1].contains(str.charAt(i) + "")){
a[1] = true;
if (a[2] || a[0]){
a[1] = a[0] = a[2] = false;
break;
}
continue;
}
if (s[2].contains(str.charAt(i) + "")){
a[2] = true;
if (a[0] || a[1]){
a[1] = a[0] = a[2] = false;
break;
}
continue;
}
}
if (a[0] || a[1] || a[2]){
list.add(str);
}
}
String[] res = new String[list.size()];
for (int i = 0; i < res.length; ++ i){
res[i] = list.get(i);
}
return res;
}
}