Back to blog
Jun 07, 2024
9 min read

Capitalize the Title

Capitalize words in a title: words longer than 2 characters have first letter uppercase, rest lowercase; words of 1-2 characters are all lowercase.

Difficulty: Easy | Acceptance: 68.00% | Paid: No Topics: String

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 rest to lowercase.

Return the capitalized title.

Examples

Input: title = "capiTalIze tHe titLe"
Output: "Capitalize The Title"
Explanation:
- "capiTalIze" has length 8, so it becomes "Capitalize".
- "tHe" has length 2, so it becomes "the".
- "titLe" has length 5, so it becomes "Title".
Input: title = "First leTTeR of EACH Word"
Output: "First Letter of Each Word"
Explanation:
- "First" has length 5, so it becomes "First".
- "leTTeR" has length 6, so it becomes "Letter".
- "of" has length 2, so it becomes "of".
- "EACH" has length 4, so it becomes "Each".
- "Word" has length 4, so it becomes "Word".
Input: title = "i lOve leetcode"
Output: "i Love Leetcode"
Explanation:
- "i" has length 1, so it becomes "i".
- "lOve" has length 4, so it becomes "Love".
- "leetcode" has length 8, so it becomes "Leetcode".

Constraints

1 <= title.length <= 100
title consists of words separated by a single space, without leading or trailing spaces.
All the letters of title are English uppercase and lowercase letters.

Split and Process

Intuition Split the title into individual words, process each word based on its length, then join them back together.

Steps

  • Split the string by spaces to get an array of words
  • For each word, check if its length is 2 or less
  • If length <= 2, convert the entire word to lowercase
  • Otherwise, capitalize the first letter and lowercase the rest
  • Join all processed words with spaces
python
class Solution:
    def capitalizeTitle(self, title: str) -> str:
        words = title.split()
        result = []
        for word in words:
            if len(word) &lt;= 2:
                result.append(word.lower())
            else:
                result.append(word[0].upper() + word[1:].lower())
        return ' '.join(result)

Complexity

  • Time: O(n) where n is the length of the title
  • Space: O(n) for storing the result array
  • Notes: Simple and readable approach with built-in string methods

Two Pointers

Intuition Use two pointers to identify word boundaries in the original string and modify characters in place, avoiding extra space for word arrays.

Steps

  • Convert string to character array for in-place modification
  • Use two pointers: start marks word beginning, i scans forward
  • When a space or end is found, calculate word length
  • Apply capitalization rules based on word length
  • Move to next word and repeat
python
class Solution:
    def capitalizeTitle(self, title: str) -> str:
        title = list(title)
        n = len(title)
        i = 0
        while i &lt; n:
            start = i
            while i &lt; n and title[i] != ' ':
                i += 1
            word_len = i - start
            if word_len &lt;= 2:
                for j in range(start, i):
                    title[j] = title[j].lower()
            else:
                title[start] = title[start].upper()
                for j in range(start + 1, i):
                    title[j] = title[j].lower()
            i += 1
        return ''.join(title)

Complexity

  • Time: O(n) where n is the length of the title
  • Space: O(n) for character array (O(1) extra space in C++)
  • Notes: More efficient memory usage, especially in C++ with string modification

Regular Expression

Intuition Use regex pattern matching to find all words and apply a replacement function that handles the capitalization rules.

Steps

  • Define a regex pattern to match non-space characters (words)
  • Use a callback/replacement function for each match
  • In the callback, check word length and apply appropriate transformation
  • Return the modified string
python
import re

class Solution:
    def capitalizeTitle(self, title: str) -> str:
        def replace_word(match):
            word = match.group()
            if len(word) &lt;= 2:
                return word.lower()
            return word[0].upper() + word[1:].lower()
        
        return re.sub(r'S+', replace_word, title)

Complexity

  • Time: O(n) where n is the length of the title
  • Space: O(n) for the result string
  • Notes: Concise solution but may have overhead from regex engine