Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 979 Bytes

Keyboard Row.md

File metadata and controls

35 lines (27 loc) · 979 Bytes

Algorithm

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"

Solution

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/Space Complexity

  • Time Complexity: O(n) where n is the number of words
  • Space Complexity: O(1)

Links