Domain 1EasyCORE

To Lower Case

The problem requires implementing a function `ToLowerCase()` that takes a string `str` as input and returns the same string converted to lowercase. The inputs are strings containing alphabetic characters, possibly mixed with uppercase and lowercase letters. The output should be the input string with all alphabetic characters converted to their lowercase equivalents.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#String

709. To Lower Case

Model: qwen2.5-coder:14b
Topics: String
FAANG asked: 1
Acceptance rate: 80.2%


Problem Breakdown

The problem requires implementing a function ToLowerCase() that takes a string str as input and returns the same string converted to lowercase. The inputs are strings containing alphabetic characters, possibly mixed with uppercase and lowercase letters. The output should be the input string with all alphabetic characters converted to their lowercase equivalents.

Inputs:

  • A string str consisting of alphabetic characters (both uppercase and lowercase).

Outputs:

  • A string where all alphabetic characters are in lowercase.

Implicit Constraints or Tricky Edge Cases:

  • The input string could be empty.
  • The input string might already be entirely in lowercase, entirely in uppercase, or a mix of both.
  • Non-alphabetic characters should remain unchanged.

Intuition

The brute-force approach would involve iterating through each character in the string and checking if it is an uppercase letter. If it is, we convert it to its corresponding lowercase letter by adjusting its ASCII value. This approach works but has a time complexity of O(n), where n is the length of the string.

A better approach leverages Python’s built-in string method lower(), which efficiently converts all alphabetic characters in a string to lowercase. This method is optimized and concise, making it preferable for this problem.

Approach

  1. Identify the Problem: Convert all uppercase letters in the input string to their corresponding lowercase letters.
  2. Choose the Algorithm/Data Structure: Use Python’s built-in lower() method, which is efficient and handles the conversion internally.
  3. Steps:
    • Call the lower() method on the input string.
    • Return the resulting string.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the input string. This is because the lower() method processes each character in the string once.
  • Space Complexity: O(n), as a new string is created to store the result, which could be up to the same length as the input string.

Solution

def ToLowerCase(s: str) -> str:
    # Convert the entire string to lowercase using Python's built-in method
    return s.lower()

Edge Cases Handled

  • Empty String: The function returns an empty string if the input is empty.
  • Already Lowercase: If the input string is already in lowercase, the function returns it unchanged.
  • Already Uppercase: If the input string is entirely uppercase, the function converts all characters to lowercase.
  • Mixed Case: If the input string contains a mix of uppercase and lowercase letters, the function converts all uppercase letters to lowercase while leaving lowercase letters unchanged.