Use a "regular expression" (regex) by using .matches().
[]
around a 'set' of characters is a match if any of the characters match+
means 1 or more of whatever precedes it. We can alternatively use*
which means 0 or more.|
means "or"
class Solution {
public String[] findWords(String[] words) {
if (words == null) {
return null;
}
List<String> list = new ArrayList();
for (String word : words) {
if (word.toLowerCase().matches("[qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+")) {
list.add(word);
}
}
return list.toArray(new String[list.size()]);
}
}
- Time Complexity: O(n) where n is the number of words
- Space Complexity: O(1)