Let's make this one less mysterious. For 520. Detect Capital, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are detectCapitalUse.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Given a word, you need to judge whether the usage of capitals in it is right or not.
03
04We define the usage of capitals in a word to be right when one of the following cases holds:
05
06All letters in this word are capitals, like "USA".
07All letters in this word are not capitals, like "leetcode".
08Only the first letter in this word is capital if it has more than one letter, like "Google".
09Otherwise, we define that this word doesn't use capitals in a right way.
10Example 1:
11Input: "USA"
12Output: True
13Example 2:
14Input: "FlaG"
15Output: False
16Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
17**/
18
19//Runtime: 8 ms, faster than 99.61% of C++ online submissions for Detect Capital.
20//Memory Usage: 11.2 MB, less than 92.50% of C++ online submissions for Detect Capital.
21
22class Solution {
23public:
24 bool detectCapitalUse(string word) {
25 if(isupper(word[0])){
26 if(word.size()<2) return true;
27 if(isupper(word[1])){
28 for(int i = 1; i < word.size(); i++){
29 if(islower(word[i])) return false;
30 }
31 }else{
32 for(int i = 1; i < word.size(); i++){
33 if(isupper(word[i])) return false;
34 }
35 }
36 }else{
37 for(int i = 1; i < word.size(); i++){
38 if(isupper(word[i])) return false;
39 }
40 }
41 return true;
42 }
43};
Cost