
Interview Problem Explained: To Lower Case (LeetCode 709, String Manipulation & ASCII Code)
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 Manipulation, ASCII Code, Built-in Functions
Clearly understand the requirements of the To Lower Case problem and set up a solution strategy.
Learn the principles of character encoding (ASCII) and how to use them to manually convert uppercase to lowercase.
Master a concise and Pythonic solution using Python's
str.lower()method.Compare the pros, cons, efficiency (time/space complexity), and robustness of both approaches.
Understand string immutability and its impact on code implementation.
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 <= 100sconsists 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
Check each character
cin the input stringsin order.Determine if
cis an uppercase alphabet (between 'A' and 'Z').If uppercase: Convert to lowercase.
If not: Leave as is.
Collect the converted or original characters in sequence to build a new string.
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.
Logic Breakdown
Create an empty list
result_charsto 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.
Iterate through each character
cin the input strings.Check if
cis uppercase using'A' <= char <= 'Z'.If uppercase: Calculate
ord(char) + 32, convert back usingchr(), and append toresult_chars.If not: Append
cas is toresult_chars.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 123Time & Space Complexity
Time Complexity: O(N). Each character is processed once.
ord(),chr(), list append, andjoin()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
Call the
lower()method on the input strings.This method automatically converts all uppercase letters to lowercase and leaves other characters unchanged, returning a new string.
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 |
|
|---|---|---|
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()andchr()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()andchr()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).
Comments 0
0/2000
More articles

Interview Problem Explained: Guess Number Higher or Lower II(LeetCode 375, Minimax & Dynamic Programming)

Interview Problem Explained: Guess Number Higher or Lower (LeetCode 374, Binary Search)

Interview Problem Explained: Minimize String Length (LeetCode 2716)

Interview Problem Explained: Ransom Note (LeetCode 383, String Manipulation & Counting Array)
Coding Interview Prep
- Interview Problem Explained: Guess Number Higher or Lower II(LeetCode 375, Minimax & Dynamic Programming)

- Interview Problem Explained: Guess Number Higher or Lower (LeetCode 374, Binary Search)

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

- Interview Problem Explained: Fibonacci Number (LeetCode 509, DP & Memoization)

- Interview Problem Explained: Minimize String Length (LeetCode 2716)
