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.
public class Solution {
public String[] findWords(String[] words) {
String[] result = null;
if (words != null) {
List<String> list = new ArrayList<String>();
String rule1 = ".*[^qwertyuiop]+?.*";
String rule2 = ".*[^asdfghjkl]+?.*";
String rule3 = ".*[^zxcvbnm]+?.*";
for (String word : words) {
String tmp = word.toLowerCase();
if (!tmp.matches(rule1) || !tmp.matches(rule2) || !tmp.matches(rule3)) {
list.add(word);
}
}
result = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
result[i] = list.get(i);
}
}
return result;
}
}