Skip to main content

Capitalize the Title - Solution & Explanation

EasyString11 min read
Practice this problem

Problem Statement

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:

  • If the length of the word is 1 or 2 letters, change all letters to lowercase.
  • Otherwise, change the first letter to uppercase and the remaining 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 <= 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.

Approach Overview

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 1: String Traversal and Manipulation

This approach involves splitting the input string into individual words, iterating over each word to check its length, and then applying the capitalization rules accordingly. The words are then concatenated back into a single string.

The function uses the C library's strtok function to split the input string based on spaces. It iterates over each word, checks its length, and applies the capitalization rules. The modified words are put back into the original string array.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of characters in the string. Space Complexity: O(1), as the transformation is done in-place.

Try this approach in the editor →

Approach 2: Using Functional Programming (for supported languages)

This approach utilizes functional programming techniques, such as map and lambdas, to apply transformations directly on the array of words extracted from the input string.

This Python solution employs a functional style using map and a lambda function to capitalize words longer than two characters, achieved concisely in one line.

Code

Python

JavaScript

Complexity

Time Complexity: O(n), where n is the number of characters in the string. Space Complexity: O(n), due to the intermediate list created.

Try this approach in the editor →

Approach 3: 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.

The time complexity is O(n), and the space complexity is O(n), where n is the length of the string title.

Code

Python

Java

C++

Go

TypeScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
String Traversal and Manipulation

Time Complexity: O(n), where n is the number of characters in the string. Space Complexity: O(1), as the transformation is done in-place.

Using Functional Programming (for supported languages)

Time Complexity: O(n), where n is the number of characters in the string. Space Complexity: O(n), due to the intermediate list created.

Simulation

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String Traversal and ManipulationO(n)O(n)Standard solution for interviews and most languages
Split and Rebuild with Functional MappingO(n)O(n)When using Python/JavaScript functional features for concise code

Video Solution

Capitalize the Title || LeetCode 2129 || Biweekly LeetCode69 || LeetCode || String || ImplementationBro Coders2,756 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Capitalize the Title easy or hard?
Capitalize the Title is classified as an Easy problem on LeetCode with an acceptance rate around 68%. It focuses on basic string processing, conditional checks, and rebuilding strings rather than advanced algorithms.
Capitalize the Title Python/Java solution
In Python, you can split the string, transform each word using a loop or list comprehension, and join them with spaces. In Java, split the string using split(" "), process each word with substring and case conversion methods, then rebuild using StringBuilder or String.join(). Both implementations run in O(n) time.
How to solve Capitalize the Title in O(n)?
Split the string into words and iterate through them once. If a word length is less than or equal to 2, convert it entirely to lowercase. Otherwise capitalize the first letter and lowercase the rest. Join the processed words with spaces to produce the final title, giving O(n) time complexity.
What is the best approach for Capitalize the Title?
The best approach is simple string traversal. Split the title into words, check the length of each word, and apply lowercase or capitalization rules accordingly. This processes every character once and runs in O(n) time with O(n) extra space to rebuild the string.
Is Capitalize the Title asked at Google/Amazon/Meta?
This problem represents a common string manipulation pattern often seen in interviews at large tech companies. Variations of word formatting and capitalization logic frequently appear in screening rounds because they test careful handling of strings and edge cases.
What data structure is used in Capitalize the Title?
The problem mainly uses strings and arrays (or lists) of words. After splitting the title by spaces, each word is processed individually and stored in a list before joining back into a single string.
What is the time complexity of Capitalize the Title?
The optimal solution runs in O(n) time where n is the length of the title string. Each character is visited at most once during splitting, transformation, and reconstruction. Space complexity is O(n) because a new string or list of words is created.

Ready to solve this problem?

Practice Capitalize the Title with our built-in code editor and test cases.

Practice on FleetCode