Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 745 Bytes

Detect Capital.md

File metadata and controls

31 lines (23 loc) · 745 Bytes

Algorithm

Use a "regular expression" (regex) by using .matches().

  • [] around a 'set' of characters is a match if any of the characters match.
    • [A-Z] matches any character from A to Z.
  • * means 0 or more of whatever precedes it
  • | means "or"
  • . matches any character

Solution

class Solution {
    public boolean detectCapitalUse(String word) {
        if (word == null) {
            return false;
        }
        return word.matches("[A-Z]*|.[a-z]*");
    }
}

Time/Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Links