LeetCode 709 - To Lower Case

The problem asks us to convert every uppercase English letter in a string into its lowercase equivalent. Any character that is already lowercase, or is not an alphabetic character at all, should remain unchanged. The input is a string s containing printable ASCII characters.

LeetCode Problem 709

Difficulty: 🟢 Easy
Topics: String

Solution

Problem Understanding

The problem asks us to convert every uppercase English letter in a string into its lowercase equivalent. Any character that is already lowercase, or is not an alphabetic character at all, should remain unchanged.

The input is a string s containing printable ASCII characters. Printable ASCII includes uppercase letters, lowercase letters, digits, punctuation, spaces, and symbols. The task is specifically concerned with uppercase English letters from 'A' to 'Z'.

The expected output is a new string where every uppercase letter has been replaced with the corresponding lowercase letter. For example, 'H' becomes 'h', 'L' becomes 'l', and so on.

The constraints are very small:

  • 1 <= s.length <= 100
  • s contains printable ASCII characters

These constraints tell us that performance is not a major concern. Even an inefficient solution would work comfortably within the limits. However, the problem is intended to test basic string manipulation and character processing.

Several edge cases are important to think about before implementation:

  • The string may already be entirely lowercase.
  • The string may contain only uppercase letters.
  • The string may contain digits or symbols that should remain unchanged.
  • The string length may be as small as 1.
  • Mixed strings with uppercase, lowercase, numbers, and punctuation should all be handled correctly.

The problem guarantees that the input contains only printable ASCII characters, so we do not need to worry about Unicode casing rules or international alphabets.

Approaches

Brute Force Approach

A straightforward brute-force solution is to iterate through every character in the string and manually check whether it is an uppercase letter. If it is uppercase, we convert it into lowercase using ASCII arithmetic. Otherwise, we leave it unchanged.

In ASCII encoding, uppercase and lowercase English letters are separated by a fixed distance:

  • 'A' to 'Z' have ASCII values 65 to 90
  • 'a' to 'z' have ASCII values 97 to 122

The difference between corresponding uppercase and lowercase letters is always 32. For example:

  • 'A''a'
  • 65 + 32 = 97

Using this property, we can convert uppercase letters manually.

This approach is correct because every character is processed independently, and the ASCII conversion rule guarantees that each uppercase letter maps to the correct lowercase version.

Even though this is called a brute-force approach, it is already efficient enough for the problem constraints.

Optimal Approach

The optimal solution uses the built-in lowercase conversion functionality provided by the programming language.

Languages such as Python and Go already provide optimized string conversion utilities:

  • Python: s.lower()
  • Go: strings.ToLower(s)

These built-in methods internally handle the conversion efficiently and clearly express the intent of the code.

The key insight is that the problem does not require us to manually implement ASCII conversion. Since the goal is simply to transform uppercase letters into lowercase letters, the standard library provides the cleanest and most maintainable solution.

Approach Time Complexity Space Complexity Notes
Brute Force O(n) O(n) Manually checks and converts uppercase characters
Optimal O(n) O(n) Uses built-in lowercase conversion utilities

Algorithm Walkthrough

Optimal Algorithm

  1. Receive the input string s.
  2. Call the language's built-in lowercase conversion function on the string.

This function scans every character in the string and converts uppercase letters into lowercase letters while leaving all other characters unchanged. 3. Return the converted string.

Why it works

The algorithm works because each uppercase English letter has a well-defined lowercase equivalent. The built-in lowercase conversion function applies this mapping consistently to every character in the string. Since non-uppercase characters are preserved unchanged, the resulting string exactly matches the problem requirements.

Python Solution

class Solution:
    def toLowerCase(self, s: str) -> str:
        return s.lower()

The implementation is intentionally simple because Python already provides a reliable built-in method for lowercase conversion.

The method receives the input string s and immediately calls s.lower().

The lower() method creates and returns a new string where every uppercase letter has been converted to lowercase. Characters that are already lowercase, as well as digits and punctuation, remain unchanged.

This directly follows the optimal algorithm described earlier.

Go Solution

package main

import "strings"

func toLowerCase(s string) string {
    return strings.ToLower(s)
}

The Go solution uses the strings.ToLower function from the standard library.

Unlike Python, Go requires importing the strings package explicitly.

The function takes the input string s and returns the lowercase-transformed version.

One important Go-specific detail is that strings are immutable, just like in Python. The strings.ToLower function therefore creates and returns a new string rather than modifying the original one.

There are no concerns about integer overflow or nil handling in this problem because the input is always a valid string and the operation is purely character-based.

Worked Examples

Example 1

Input:

s = "Hello"

Step-by-step processing:

Character Is Uppercase? Converted Character Result So Far
H Yes h "h"
e No e "he"
l No l "hel"
l No l "hell"
o No o "hello"

Final output:

"hello"

Example 2

Input:

s = "here"

Step-by-step processing:

Character Is Uppercase? Converted Character Result So Far
h No h "h"
e No e "he"
r No r "her"
e No e "here"

Final output:

"here"

Example 3

Input:

s = "LOVELY"

Step-by-step processing:

Character Is Uppercase? Converted Character Result So Far
L Yes l "l"
O Yes o "lo"
V Yes v "lov"
E Yes e "love"
L Yes l "lovel"
Y Yes y "lovely"

Final output:

"lovely"

Complexity Analysis

Measure Complexity Explanation
Time O(n) Every character in the string is processed once
Space O(n) A new lowercase string is created

The algorithm must examine every character in the input string to determine whether it needs conversion. Since each character is processed exactly once, the time complexity is linear in the length of the string.

The space complexity is also linear because strings are immutable in both Python and Go, so a new string must be allocated for the result.

Test Cases

solution = Solution()

assert solution.toLowerCase("Hello") == "hello"  # mixed uppercase and lowercase
assert solution.toLowerCase("here") == "here"  # already lowercase
assert solution.toLowerCase("LOVELY") == "lovely"  # all uppercase
assert solution.toLowerCase("A") == "a"  # single uppercase character
assert solution.toLowerCase("z") == "z"  # single lowercase character
assert solution.toLowerCase("123") == "123"  # digits remain unchanged
assert solution.toLowerCase("Hello123") == "hello123"  # letters and digits
assert solution.toLowerCase("TeSt!") == "test!"  # punctuation remains unchanged
assert solution.toLowerCase(" ") == " "  # single space character
assert solution.toLowerCase("ABCxyz") == "abcxyz"  # mixed uppercase and lowercase
Test Why
"Hello" Validates normal mixed-case conversion
"here" Confirms lowercase strings remain unchanged
"LOVELY" Tests conversion of all uppercase letters
"A" Smallest uppercase input
"z" Smallest lowercase input
"123" Ensures digits are unaffected
"Hello123" Verifies mixed alphanumeric handling
"TeSt!" Confirms punctuation remains unchanged
" " Tests printable whitespace character
"ABCxyz" Tests mixed uppercase and lowercase letters

Edge Cases

Strings Already in Lowercase

An input string may already contain only lowercase letters. A buggy implementation might unnecessarily alter characters or produce incorrect output. The current implementation handles this correctly because lowercase characters are left unchanged by the conversion function.

Strings Containing Non-Alphabetic Characters

The input may contain digits, punctuation, or spaces. These characters do not have uppercase or lowercase forms, so they must remain exactly as they are. Built-in lowercase conversion functions correctly preserve these characters without modification.

Single Character Strings

The smallest valid input has length 1. A careless implementation could fail due to indexing assumptions or loop logic. The provided solution works correctly even for single-character strings because the conversion function naturally handles strings of any valid length.