Watch 10 video solutions for Capitalize the Title, a easy level problem involving String. This walkthrough by Bro Coders has 2,707 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string title consisting of one or more words separated by a single space, where each word consists of English letters. Capitalize the string by changing the capitalization of each word such that:
1 or 2 letters, change all letters to lowercase.Return the capitalized title.
Example 1:
Input: title = "capiTalIze tHe titLe" Output: "Capitalize The Title" Explanation: 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" Explanation: 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" Explanation: 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 <= 100title consists of words separated by a single space without any leading or trailing spaces.Problem Overview: You receive a title string made of words separated by spaces. Words with length <= 2 must remain fully lowercase, while longer words must have the first letter uppercase and the rest lowercase. The task is straightforward string processing: scan each word, apply the rule, and rebuild the title.
Approach 1: String Traversal and Manipulation (O(n) time, O(n) space)
Split the title into words using spaces, then iterate through each word. For every word, check its length. If the length is <= 2, convert the entire word to lowercase using a built‑in function like lower(). Otherwise, capitalize the first character and lowercase the remaining characters using operations such as word[0].upper() and word[1:].lower(). Append the processed word to a result list and finally join the words with spaces to rebuild the string.
This approach relies on basic string manipulation operations and a single pass over the input. Each character is processed at most once, giving O(n) time where n is the length of the title. Because we store split words and rebuild the string, the extra memory cost is O(n). This is the most direct and readable solution and works consistently across languages like C++, Java, Python, and JavaScript.
Approach 2: Functional Programming (O(n) time, O(n) space)
Many languages support functional utilities such as map or list comprehensions. First split the title into words. Then apply a transformation function to each word: if its length is <= 2, return the lowercase version; otherwise return a new string with the first character uppercase and the rest lowercase. The map operation processes each word independently, and the results are joined with spaces at the end.
The key insight is treating the transformation as a pure function applied to every word. Languages like Python and JavaScript make this concise using list comprehensions or Array.map(). Complexity remains O(n) time and O(n) space since every character is visited once and a new string is constructed.
Recommended for interviews: The string traversal approach is what interviewers expect. It shows you can iterate through tokens, apply conditional logic, and rebuild strings efficiently. The functional style is elegant and concise but relies on language features rather than demonstrating step‑by‑step string processing logic. Showing the iterative solution first proves you understand the underlying mechanics.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Traversal and Manipulation | O(n) | O(n) | Standard solution for interviews and most languages |
| Split and Rebuild with Functional Mapping | O(n) | O(n) | When using Python/JavaScript functional features for concise code |