Detect Capital - Problem
Detect Capital - String Pattern Recognition

You're a text editor developer implementing a smart capitalization checker! Your task is to determine if a word follows proper capitalization rules.

Valid Capitalization Patterns:
๐Ÿ”น All uppercase: "USA", "NASA"
๐Ÿ”น All lowercase: "hello", "world"
๐Ÿ”น Title case: "Google", "Apple"

Goal: Return true if the word follows one of these patterns, false otherwise.

Think of it like a grammar checker that validates proper noun formatting!

Input & Output

example_1.py โ€” All Uppercase
$ Input: word = "USA"
โ€บ Output: true
๐Ÿ’ก Note: All letters are uppercase, which is one of the valid capitalization patterns.
example_2.py โ€” Title Case
$ Input: word = "Google"
โ€บ Output: true
๐Ÿ’ก Note: Only the first letter is uppercase and the rest are lowercase, forming valid title case.
example_3.py โ€” Invalid Mixed Case
$ Input: word = "FlaG"
โ€บ Output: false
๐Ÿ’ก Note: Mixed capitalization that doesn't follow any valid pattern - not all uppercase, not all lowercase, and not title case.

Visualization

Tap to expand
Capitalization Pattern LogicAll UppercaseCount = Length"USA", "NASA"โœ“ VALIDAll LowercaseCount = 0"hello", "world"โœ“ VALIDTitle CaseCount = 1"Google", "Apple"โœ“ VALIDInvalid Mixed CaseCount > 1 AND Count < Length"FlaG", "wORd", "TeSt"โœ— INVALIDAlgorithm Decision TreeIf (count == length) โ†’ All Upper โœ“Else if (count == 0) โ†’ All Lower โœ“Else if (count == 1 && first is upper) โ†’ Title โœ“Else โ†’ Invalid โœ—
Understanding the Visualization
1
Count the Uniforms
Count how many team members are wearing formal uniforms (uppercase letters)
2
Check the Captain
See if the team captain (first letter) is wearing a formal uniform
3
Determine Team Status
Valid teams: all formal, all casual, or captain formal with casual team
Key Takeaway
๐ŸŽฏ Key Insight: Only three valid patterns exist, and counting uppercase letters with first letter check covers all cases efficiently in O(n) time.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the string of length n

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for counter and variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค word.length โ‰ค 100
  • word consists of lowercase and uppercase English letters only
  • No empty strings or special characters
Asked in
Google 25 Amazon 20 Microsoft 15 Apple 10
28.5K Views
Medium Frequency
~8 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen