Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Capitalize the Title

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2129" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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.

Explanation

Here's a breakdown of the approach and the Python code:

  • Split the string: Split the input string into a list of words using the space as a delimiter.
  • Iterate and capitalize/lowercase: Iterate through the list of words. For each word, check its length. If the length is 1 or 2, convert the entire word to lowercase. Otherwise, convert the first letter to uppercase and the rest to lowercase.
  • Join the words: Finally, join the modified words back together with spaces to form the capitalized title.

  • Runtime Complexity: O(n), where n is the length of the input string.

  • Storage Complexity: O(n), due to the space used to store the list of words and the modified string.

Code

    def capitalizeTitle(title: str) -> str:
    words = title.split()
    result = []
    for word in words:
        if len(word) <= 2:
            result.append(word.lower())
        else:
            result.append(word[0].upper() + word[1:].lower())
    return " ".join(result)

More from this blog

C

Chatmagic blog

2894 posts