The Detect Capital problem asks you to determine whether the usage of capital letters in a given word is correct. According to the problem, a word is said to use capitals correctly if one of the following conditions is true:
"USA"
)."leetcode"
)."Google"
).
You are given a single string word
and need to return true
if it uses capital letters correctly, and false
otherwise.
The problem is about checking if a word follows one of three rules regarding capitalization. The brute-force way would be to check each character and compare its case. However, we can use built-in string methods to check each condition efficiently.
We don't need to check every possible combination of uppercase and lowercase letters. Instead, we focus on these three valid patterns, which simplifies the logic and avoids unnecessary complexity.
Let's break down the algorithm step by step:
isupper()
method (or equivalent) to check if the entire string is in uppercase.islower()
method to check if all characters are lowercase.This approach is efficient because it leverages string methods optimized in the standard library and avoids manual iteration or unnecessary checks.
Let's walk through an example with the input word = "FlaG"
:
"FlaG".isupper()
returns false
."FlaG".islower()
returns false
."FlaG"[0].isupper()
is True
(since 'F' is uppercase)."FlaG"[1:].islower()
is False
(since 'l' is lowercase, but 'a' is lowercase, and 'G' is uppercase).false
.
For word = "Google"
:
false
.false
."Google"[0].isupper()
is True
, "Google"[1:].islower()
is True
.true
.class Solution:
def detectCapitalUse(self, word: str) -> bool:
return (
word.isupper() or
word.islower() or
(word[0].isupper() and word[1:].islower())
)
class Solution {
public:
bool detectCapitalUse(string word) {
if (all_of(word.begin(), word.end(), ::isupper)) return true;
if (all_of(word.begin(), word.end(), ::islower)) return true;
if (isupper(word[0]) && all_of(word.begin() + 1, word.end(), ::islower)) return true;
return false;
}
};
class Solution {
public boolean detectCapitalUse(String word) {
if (word.equals(word.toUpperCase())) return true;
if (word.equals(word.toLowerCase())) return true;
if (Character.isUpperCase(word.charAt(0)) && word.substring(1).equals(word.substring(1).toLowerCase())) return true;
return false;
}
}
var detectCapitalUse = function(word) {
return (
word === word.toUpperCase() ||
word === word.toLowerCase() ||
(word[0] === word[0].toUpperCase() && word.slice(1) === word.slice(1).toLowerCase())
);
};
Brute-force approach:
isupper()
, islower()
) or equivalent scans the string once, so the time complexity is O(n), where n is the length of the input word.Thus, the solution is efficient and optimal for this problem.
The Detect Capital problem boils down to checking if a word fits one of three capitalization patterns. By leveraging built-in string methods, we can check each rule simply and efficiently, avoiding unnecessary manual iteration. The solution is concise, readable, and runs in linear time with constant space. This approach highlights the power of understanding both the problem constraints and the language's standard library.