LeetCode Problem Workspace
Capitalize the Title
Capitalize the Title requires transforming each word based on length, applying uppercase and lowercase rules precisely per word.
1
Topics
6
Code langs
3
Related
Practice Focus
Easy · String-driven solution strategy
Answer-first summary
Capitalize the Title requires transforming each word based on length, applying uppercase and lowercase rules precisely per word.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for String-driven solution strategy
This problem focuses on applying a string-driven solution strategy to manipulate word capitalization based on word length. Words of length one or two remain lowercase, while words of length three or more capitalize the first letter. Efficient iteration over the words ensures accurate transformation while adhering to the string pattern constraints.
Problem Statement
Given a string title containing one or more words separated by single spaces, transform each word based on its length. Words with fewer than three letters must remain lowercase, while words with three or more letters should capitalize the first letter and lowercase the rest.
Return the resulting capitalized string. For example, title = "capiTalIze tHe titLe" should return "Capitalize The Title", adjusting each word according to the capitalization rules.
Examples
Example 1
Input: title = "capiTalIze tHe titLe"
Output: "Capitalize The Title"
Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.
Example 2
Input: title = "First leTTeR of EACH Word"
Output: "First Letter of Each Word"
The word "of" has length 2, so it is all lowercase. The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
Example 3
Input: title = "i lOve leetcode"
Output: "i Love Leetcode"
The word "i" has length 1, so it is lowercase. The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
Constraints
- 1 <= title.length <= 100
- title consists of words separated by a single space without any leading or trailing spaces.
- Each word consists of uppercase and lowercase English letters and is non-empty.
Solution Approach
Split the string into words
Use a standard string split operation on spaces to extract all words. This isolates each word to apply the length-based capitalization rules efficiently.
Apply capitalization rules per word
Iterate through each word and check its length. If the length is 1 or 2, convert the word to lowercase. Otherwise, capitalize the first character and lowercase the remaining characters.
Recombine words into the final title
Join the processed words back into a single string using spaces. This produces the correctly capitalized title while preserving the original word order.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(n) where n is the length of the title, since each character is visited once. Space complexity is O(n) for storing the split words and the result string.
What Interviewers Usually Probe
- They may ask if your solution handles single-letter words correctly.
- They may probe how your solution deals with words exactly two characters long.
- Expect questions on edge cases like all uppercase or all lowercase input.
Common Pitfalls or Variants
Common pitfalls
- Incorrectly capitalizing words with length 1 or 2.
- Failing to lowercase letters after the first character for longer words.
- Not preserving the original word order when joining the words back.
Follow-up variants
- Allowing punctuation within words and capitalizing based on alphabetic characters only.
- Ignoring case of the original input entirely before applying rules.
- Applying the same rules to hyphenated words as separate words.
FAQ
What is the main pattern used in Capitalize the Title?
The problem uses a string-driven solution strategy focusing on per-word capitalization rules based on word length.
How should words of length 1 or 2 be capitalized?
Words of length 1 or 2 must remain entirely lowercase regardless of their original case.
Can this solution handle titles with mixed casing?
Yes, the approach first isolates words and then applies consistent capitalization rules, handling any mix of uppercase and lowercase letters.
What is the time complexity for Capitalize the Title?
Time complexity is O(n), where n is the length of the title string, as each character is processed once.
Are spaces between words preserved?
Yes, words are joined with single spaces after applying capitalization, preserving the original spacing pattern.
Solution
Solution 1: Simulation
Directly simulate the process. Split the string by spaces to get each word, then convert each word to the appropriate case as per the problem statement. Finally, join the words with spaces.
class Solution:
def capitalizeTitle(self, title: str) -> str:
words = [w.lower() if len(w) < 3 else w.capitalize() for w in title.split()]
return " ".join(words)Continue Topic
string
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
String-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward