Skip to main content

To Lower Case - Solution & Explanation

EasyString10 min readAsked at: Microsoft, Meta, Google
Practice this problem

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.

Approach Overview

Problem Overview: Given a string s, convert every uppercase English letter to its lowercase equivalent and return the resulting string. Characters that are already lowercase or not alphabetic remain unchanged. The task focuses on character manipulation within a string while maintaining linear performance.

Approach 1: Use Built-In String Function (O(n) time, O(n) space)

Most languages provide a standard library function such as toLowerCase() or lower(). This method internally iterates through the string and converts each uppercase character to its lowercase counterpart based on the language’s character encoding rules. The implementation is usually optimized in native libraries, so it is both reliable and concise.

You simply call the built-in method on the string and return the result. Internally the operation scans all n characters once, giving O(n) time complexity. A new string is typically created during conversion, which results in O(n) space complexity. For most production code and interviews, this is the cleanest solution because it leverages tested library functionality instead of reimplementing character logic.

Approach 2: Manual ASCII Conversion (O(n) time, O(n) space)

This approach manually converts characters using ASCII values. In ASCII encoding, uppercase letters 'A' to 'Z' map to values 65–90, while lowercase letters 'a' to 'z' map to 97–122. The difference between the uppercase and lowercase version of the same letter is exactly 32.

Iterate through the string character by character. For each character, check if it lies between 'A' and 'Z'. If it does, convert it by adding 32 to its ASCII value (or equivalently shifting to the lowercase range). Append the converted character to a result string; otherwise append the character unchanged. This technique demonstrates understanding of string processing and low-level character operations.

The algorithm performs a single pass through the string, so the time complexity remains O(n). Because most implementations build a new result string or character array, the space complexity is O(n). Some languages allow in-place modification using mutable arrays, but the asymptotic complexity generally stays the same.

Recommended for interviews: Interviewers typically expect the built-in function first because it reflects practical engineering habits. Mentioning the ASCII conversion approach shows deeper understanding of character encoding and how string transformations work internally. Demonstrating both approaches signals that you know when to rely on language libraries and when to implement logic manually.

Approach 1: Use Built-In String Function

This approach utilizes the built-in function available in standard libraries of most programming languages to convert strings to lowercase. Such functions are usually optimized and provide a direct solution to the problem by iterating over each character in the string and converting it to its lowercase equivalent if it's uppercase.

In this C example, the tolower() function from ctype.h is used. It converts each character of the string to lowercase in-place if it is an uppercase letter.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the string, as we iterate through the string once.
Space Complexity: O(1), as the conversion is done in-place without any additional space.

Try this approach in the editor →

Approach 2: Manual ASCII Conversion

This approach manually converts each character by examining its ASCII value. If a character is uppercase (between 65, 'A', and 90, 'Z'), we add 32 to convert it to its lowercase counterpart (between 97, 'a', and 122, 'z'). This method ensures you understand converting characters without relying on the language built-in functions.

This solution iterates over the string and checks each character. If it's an uppercase letter, it adds 32 (difference in ASCII values between uppercase and lowercase letters) to convert it to lowercase.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), due to iterating through each character.
Space Complexity: O(1), since the conversion happens in-place.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Use Built-In String Function

Time Complexity: O(n), where n is the length of the string, as we iterate through the string once.
Space Complexity: O(1), as the conversion is done in-place without any additional space.

Manual ASCII Conversion

Time Complexity: O(n), due to iterating through each character.
Space Complexity: O(1), since the conversion happens in-place.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Built-In String FunctionO(n)O(n)Preferred in production and interviews when standard library utilities are allowed
Manual ASCII ConversionO(n)O(n)Useful when implementing character transformations manually or when library functions are restricted

Video Solution

LeetCode Problem: 709. To Lower Case | Java Solution • Code for Interview • 61,322 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is To Lower Case easy or hard?
To Lower Case is classified as an Easy problem. It focuses on basic string traversal and character conversion logic, making it a common introductory exercise for string manipulation.
To Lower Case Python/Java solution
Python uses the built-in method s.lower(), which converts all uppercase letters to lowercase in O(n) time. In Java, the equivalent method is s.toLowerCase(). Both rely on optimized standard library implementations for character transformation.
How to solve To Lower Case in O(n)?
Iterate through the string once and convert uppercase characters to lowercase. Using built-in functions like lower() or toLowerCase() achieves this directly. A manual approach checks if a character lies between 'A' and 'Z' and adds 32 to its ASCII value to convert it.
What is the best approach for To Lower Case?
The built-in string conversion function such as lower() in Python or toLowerCase() in Java is the best approach. It converts every character in a single pass with O(n) time complexity and uses optimized standard library implementations. This approach keeps the code concise and reliable.
Is To Lower Case asked at Google/Amazon/Meta?
To Lower Case is categorized as an easy string manipulation problem and is commonly used for basic screening or warm-up rounds. Similar character processing questions appear in interviews at companies like Amazon, Google, and Meta to test familiarity with string operations.
What data structure is used in To Lower Case?
The problem primarily uses a string or a character array. The algorithm iterates through characters sequentially and either modifies them directly or constructs a new string with converted characters.
What is the time complexity of To Lower Case?
The time complexity is O(n), where n is the length of the string. Each character must be inspected at least once to determine whether it should be converted. Both the built-in method and manual ASCII conversion operate in linear time.

Ready to solve this problem?

Practice To Lower Case with our built-in code editor and test cases.

Practice on FleetCode