Watch 10 video solutions for To Lower Case, a easy level problem involving String. This walkthrough by Code for Interview has 58,431 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |