{x}
blog image

Capitalize the Title

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.

Solution Explanation: Capitalize the Title

The problem requires capitalizing a title string according to specific rules: words with length 1 or 2 are converted to lowercase, while words with length 3 or more have their first letter capitalized and the rest lowercased.

Approach:

The most efficient approach involves these steps:

  1. Split the string: The input string title is split into individual words using space as the delimiter. This can be done using built-in string functions like split() in Python, split(" ") in Java, or similar functions in other languages.

  2. Process each word: Each word is then processed individually:

    • Check word length: The length of the word is checked.
    • Apply capitalization rules: If the length is less than 3, the word is converted to lowercase using functions like lower() or toLowerCase(). Otherwise, the first character is capitalized using upper() or toUpperCase() and the rest is converted to lowercase. Functions like capitalize() or title() can simplify this step in some languages.
  3. Join the words: The processed words are then joined back together using spaces to form the final capitalized title string. This can be done using join() or similar methods.

Time and Space Complexity Analysis:

  • Time Complexity: The algorithm iterates through the words of the input string once for splitting, and once more for processing each word. The string manipulation operations (lowercase, uppercase, etc.) typically take time proportional to the length of the word. Therefore, the overall time complexity is O(n), where n is the length of the input string title.

  • Space Complexity: The space complexity is also O(n) because:

    • We create a list or array to store the processed words (this takes space proportional to the number of words).
    • In some implementations, intermediate strings might be created during the processing, again proportional to the input string length.

Code Examples (with explanations):

The provided code examples demonstrate the solution in various programming languages. Here's a breakdown of one example (Python) and the key differences in others:

Python:

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)
  • title.split(): Splits the title string into a list of words.
  • [w.lower() if len(w) < 3 else w.capitalize() for w in ... ]: This is a list comprehension. It efficiently iterates through the words, applying the lowercase or capitalize logic based on the word length. w.lower() converts to lowercase, and w.capitalize() capitalizes the first letter and lowercases the rest.
  • " ".join(words): Joins the processed words back into a string, separated by spaces.

Java:

Java uses more explicit looping and conditional statements but follows the same logic:

class Solution {
    public String capitalizeTitle(String title) {
        List<String> ans = new ArrayList<>();
        for (String s : title.split(" ")) { //Iterates through words
            if (s.length() < 3) {
                ans.add(s.toLowerCase());
            } else {
                ans.add(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
            }
        }
        return String.join(" ", ans);
    }
}

Other languages (C++, Go, TypeScript, C#) follow similar patterns, adapting the syntax to their specific string manipulation functions and data structures. The core algorithm remains consistent across all examples.