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
class Solution {
public boolean detectCapitalUse(String word) {
if (word == null) {
return false;
}
return word.matches("[A-Z]*|.[a-z]*");
}
}
- Time Complexity: O(n)
- Space Complexity: O(1)