Interview Problem Explained: To Lower Case (LeetCode 709, String Manipulation & ASCII Code)
Coding Interview Prep

Interview Problem Explained: To Lower Case (LeetCode 709, String Manipulation & ASCII Code)

Codetree|5 min read|Jun 9, 2025

Learn how to solve LeetCode 709: To Lower Case using both ASCII manipulation and Python's lower() method. Efficient, simple, and interview-ready.

What You’ll Learn from This Interview Prep Article

Level: Easy | Reading Time: ~7 minutes

Key Concept: String ManipulationASCII CodeBuilt-in Functions

  1. Clearly understand the requirements of the To Lower Case problem and set up a solution strategy.

  2. Learn the principles of character encoding (ASCII) and how to use them to manually convert uppercase to lowercase.

  3. Master a concise and Pythonic solution using Python's str.lower() method.

  4. Compare the pros, cons, efficiency (time/space complexity), and robustness of both approaches.

  5. Understand string immutability and its impact on code implementation.

  6. Build fundamentals of string processing and learn key explanation points for coding interviews.

To Lower Case: Problem Statement

Given a string s, write a function that returns a new string with all uppercase letters in s converted to lowercase.

Examples:

  • Input: s = "Hello" / Output: "hello"

  • Input: s = "here" / Output: "here"

  • Input: s = "LOVELY" / Output: "lovely"

Constraints:

  • 1 <= s.length <= 100

  • s consists of printable ASCII characters only.

Tip: This is LeetCode Problem 709.  Codetree provides more intuitive examples and step-by-step approaches.

Step 1. Naive Approach

Idea

The core of this problem is to iterate through each character in the input string and check if it is an uppercase alphabet. If so, convert it to lowercase; otherwise, leave it unchanged. All other characters (lowercase, digits, symbols, etc.) should remain as they are.

Logic Breakdown

  1. Check each character c in the input string s in order.

  2. Determine if c is an uppercase alphabet (between 'A' and 'Z').

  3. If uppercase: Convert to lowercase.

  4. If not: Leave as is.

  5. Collect the converted or original characters in sequence to build a new string.

  6. Return the final string.

Let’s look at two main ways to implement this conversion logic.

Step 2: Manual Conversion Using ASCII Codes (O(N))

Idea

Characters are represented as numbers in computers, and the ASCII code is the standard mapping between characters and numbers. ASCII Table shows that uppercase letters 'A'-'Z' are ASCII 65-90, and lowercase 'a'-'z' are 97-122. The difference between them is exactly 32 (ord('a') - ord('A') == 32). Using this, you can convert an uppercase letter to lowercase by adding 32 to its ASCII value.

What are ord and chr?

Logic Breakdown

  1. Create an empty list result_chars to store the result.

    • In Python, when you need to modify a string, it’s common to build a list and then use "".join() to create the final string.

  2. Iterate through each character c in the input string s.

  3. Check if c is uppercase using 'A' <= char <= 'Z'.

  4. If uppercase: Calculate ord(char) + 32, convert back using chr(), and append to result_chars.

  5. If not: Append c as is to result_chars.

  6. After the loop, use "".join(result_chars) to combine the list into a string and return it.

Code Implementation (Python)

def toLowerCase_ascii(s: str) -> str:
    """
    Converts uppercase letters to lowercase using ASCII codes.
    """
    result_chars = []
    for char in s:
        # Check if the character is uppercase
        if 'A' <= char <= 'Z':
            # Convert to lowercase by adding 32 to ASCII value
            result_chars.append(chr(ord(char) + 32))
        else:
            # Leave unchanged if not uppercase
            result_chars.append(char)

    return "".join(result_chars)

# Example
print(toLowerCase_ascii("Hello World 123"))
# Output: hello world 123

Time & Space Complexity

  • Time Complexity: O(N). Each character is processed once. ord()chr(), list append, and join() are all O(N) or constant time.

  • Space Complexity: O(N). The result list and final string require space proportional to the input.

Step 3: Using Python’s Built-in lower() Method (O(N))

Idea

Python provides powerful built-in string methods. For common tasks like case conversion, using a built-in is simpler, safer, and often faster than manual implementation. The str.lower() method is designed for this.

Advantages of lower():

  • Conciseness: Implemented in a single line.

  • Readability: Instantly clear to anyone reading the code.

  • Robustness: Handles not only ASCII but also various Unicode characters (e.g., German ß, French É) correctly. This is not possible with the ASCII +32 method.

  • Performance: Usually implemented in C for high efficiency.

Logic Breakdown

  1. Call the lower() method on the input string s.

  2. This method automatically converts all uppercase letters to lowercase and leaves other characters unchanged, returning a new string.

  3. Return the resulting string.

Code Implementation (Python)

def toLowerCase_builtin(s: str) -> str:
    """
    Converts to lowercase using Python's built-in lower() method.
    """
    return s.lower()

# Example
print(toLowerCase_builtin("LOVELY PYTHON!"))
# Output: lovely python!

Time & Space Complexity

  • Time Complexity: O(N). The built-in function still iterates through the string.

  • Space Complexity: O(N)lower() creates and returns a new string.

ASCII Method vs. Built-in Function: Which to Choose?

Feature

Manual ASCII

str.lower() Built-in

Conciseness

Low

High

Readability

Medium

High

Accuracy

ASCII only

Unicode supported (High)

Performance

Good

Excellent (typically)

Development Speed

Low

High

Recommended Use

Learning, when built-ins are disallowed

Most real-world and interview scenarios

Conclusion:

Unless there are special constraints (e.g., “no built-in functions”), using str.lower() is overwhelmingly the best choice for readability, conciseness, accuracy, and performance. In interviews, it’s standard to suggest this first.

Frequently Asked Questions

Q. Why learn ASCII conversion if lower() exists?

A. Knowing ASCII is useful for understanding character encoding fundamentals, for implementing in other languages (like C/C++), and for situations where built-ins are restricted. It also demonstrates versatility and deeper understanding in interviews.

Q. What happens to non-alphabetic characters (digits, symbols, etc.)?

A. Both methods leave non-uppercase letters unchanged. The ASCII method checks 'A' <= char <= 'Z', and lower() only affects uppercase letters.

Q. In Python, is it better to concatenate strings directly (result += char) or use a list and join?

A. Python strings are immutable. Using result += char creates a new string each time and can degrade to O(N^2) performance. Building a list and joining at the end is O(N) and much more efficient. (For small strings like this problem’s constraints, the difference is minor, but it’s a good habit.)

How to Prepare for Interviews with the To Lower Problem

To Lower Case is a great question for testing basic string manipulation skills. While simple, it’s an opportunity to show understanding of how computers process characters and to compare different approaches.

Interview Tips:

  • Present multiple approaches: Offer both the built-in and manual (ASCII) solutions, and clearly explain their pros, cons, and when to use each.

  • Explain the principles: When using ASCII, describe how ord() and chr() work and the ASCII 32 difference for case conversion.

  • Discuss efficiency: Analyze time/space complexity and mention string immutability and why list+join is efficient.

  • Discuss robustness: If asked about Unicode, explain why the built-in method is superior.

  • Consider test cases: After coding, discuss how your solution handles edge cases (empty string, already lowercase, numbers/symbols, etc.).

Similar Problems to Practice

All of these problems are well-suited for practicing how to identify simple rules, discover mathematical patterns, and generalize them through mathematical reasoning.

Coding Interview Prep Guide

  • What you should be able to explain:

    • What is ASCII code, and how are uppercase and lowercase letters related?

    • How do ord() and chr() work?

    • What does Python’s str.lower() do, and what are its advantages?

    • What are the time/space complexities and pros/cons of both approaches?

    • What is string immutability in Python, and how does it affect string operations?

  • Interview tips:

    • Clarify requirements: Double-check input types (ASCII? Unicode?) and constraints.

    • Start with the simplest answer: Offer s.lower() first, then discuss manual methods if prompted.

    • Explain confidently: For simple problems, clarity and confidence in your explanation are key.

    • Code quality: Even for short code, use clear variable names, comments, and efficient patterns (list+join).

Share
Tags
AlgorithmsAlgorithmic Data Structurelearning codingfor beginnerscoding platformsleetcode problem

Comments 0

0/2000

Loading...

More articles

Coding Interview Prep

1 / 3