Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: To Lower Case

Updated
2 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "709" 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

Given a string s, return the string after replacing every uppercase letter with the same lowercase letter. Example 1: Input: s = "Hello" Output: "hello" Example 2: Input: s = "here" Output: "here" Example 3: Input: s = "LOVELY" Output: "lovely" Constraints: 1 <= s.length <= 100 s consists of printable ASCII characters.

Explanation

  • Iterate through the input string character by character.
    • If a character is an uppercase letter, convert it to lowercase using the ASCII value difference.
    • Append the converted or original character to a new string builder.
  • Runtime Complexity: O(n), Storage Complexity: O(n)

Code

    def to_lower_case(s: str) -> str:
    """
    Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.

    Example 1:
    Input: s = "Hello"
    Output: "hello"

    Example 2:
    Input: s = "here"
    Output: "here"

    Example 3:
    Input: s = "LOVELY"
    Output: "lovely"

    Constraints:
    1 <= s.length <= 100
    s consists of printable ASCII characters.
    """
    result = ""
    for char in s:
        if 'A' <= char <= 'Z':
            result += chr(ord(char) + 32)
        else:
            result += char
    return result

More from this blog

C

Chatmagic blog

2894 posts