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 <= 100s consists of printable ASCII characters.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.
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.
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.
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.
Time Complexity: O(n), due to iterating through each character.
Space Complexity: O(1), since the conversion happens in-place.
| Approach | Complexity |
|---|---|
| Use Built-In String Function | Time Complexity: O(n), where n is the length of the string, as we iterate through the string once. |
| Manual ASCII Conversion | Time Complexity: O(n), due to iterating through each character. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Built-In String Function | O(n) | O(n) | Preferred in production and interviews when standard library utilities are allowed |
| Manual ASCII Conversion | O(n) | O(n) | Useful when implementing character transformations manually or when library functions are restricted |
LeetCode Problem: 709. To Lower Case | Java Solution • Code for Interview • 58,431 views views
Watch 9 more video solutions →Practice To Lower Case with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor