500. Keyboard Row
代码链接
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.
American keyboard
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.
判断是否在一行
public String[] solution(String[] input){
List<String> list = new ArrayList<String>();
String u ="qwertyuiop";
String m ="asdfghjkl";
String d ="zxcvbnm";
for (int i = 0; i < input.length; i++) {
int before = 0;
int next = 0;
boolean bflag = true;
char[] chars = input[i].toLowerCase().toCharArray();
for (int j = 0; j < chars.length; j++) {
if(contain(u,chars[j])){
next =0;
}else if(contain(m,chars[j])){
next =1;
}else if(contain(d,chars[j])){
next =2;
}
if(j > 0 && before != next){
bflag = false;
break;
}
before = next;
}
if(bflag){
list.add(input[i]);
}
}
return list.toArray(new String[list.size()]);
}
boolean contain(String s,char c){
for (char c1 : s.toCharArray()) {
if(c1 == c){
return true;
}
}
return false;
}
public static void main(String[] args) {
KeyboardRow keyboardRow = new KeyboardRow();
String[] solution = keyboardRow.solution(new String[]{"asd", "qwe"});
for (String s : solution) {
System.out.println(s);
}
}