Difficulty Level: 🟢 Beginner
Domain: String Processing (Validation)
Given a word, determine if its capitalization is used correctly. We define correct capitalization as follows:
- All letters in the word are uppercase (e.g.,
"USA"
). - All letters in the word are lowercase (e.g.,
"leetcode"
). - Only the first letter in the word is uppercase (e.g.,
"Google"
).
If any of these conditions are met, return True
. Otherwise, return False
.
To validate correct capitalization, we simply need to check three conditions for the input word:
- All Uppercase: Every letter in the word is capital.
- All Lowercase: Every letter in the word is lowercase.
- Title Case: Only the first letter is uppercase, and all other letters are lowercase.
Python provides straightforward string methods (isupper
, islower
, and istitle
) to evaluate each of these cases, allowing for an efficient solution.
-
Input:
"USA"
-
Processing:
isupper()
returnsTrue
because all letters are uppercase.- Since one condition is satisfied, the function returns
True
.
-
Output:
True
-
Input:
"Google"
-
Processing:
istitle()
returnsTrue
since only the first letter is uppercase.- The function returns
True
.
-
Output:
True
-
Input:
"flaG"
-
Processing:
- None of the conditions (
isupper
,islower
,istitle
) are satisfied. - The function returns
False
.
- None of the conditions (
-
Output:
False