Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

709. To Lower Case - Leetcode Solution

Code Implementation

class Solution:
    def toLowerCase(self, s: str) -> str:
        return s.lower()
      
class Solution {
public:
    string toLowerCase(string s) {
        for (char &c : s) {
            if (c >= 'A' && c <= 'Z') {
                c = c - 'A' + 'a';
            }
        }
        return s;
    }
};
      
class Solution {
    public String toLowerCase(String s) {
        return s.toLowerCase();
    }
}
      
/**
 * @param {string} s
 * @return {string}
 */
var toLowerCase = function(s) {
    return s.toLowerCase();
};
      

Problem Description

The "To Lower Case" problem asks you to convert all uppercase letters in a given string s to their corresponding lowercase letters and return the resulting string. Non-alphabetic characters and lowercase letters should remain unchanged. You are guaranteed that the input will always be a valid string, and you should return a new string where all alphabetic characters are in lowercase.

  • Input: a string s
  • Output: the same string, but with all uppercase English letters converted to lowercase
  • Constraints: Only alphabetic characters ('A'-'Z', 'a'-'z') need conversion; all others remain the same
  • There is exactly one valid output for every input

Thought Process

At first glance, this problem seems simple: just change all the uppercase letters to lowercase. The most direct way is to use a built-in function, if available. However, if we were to do it manually, we would need to check each character, determine if it is uppercase, and convert it if necessary. This leads to thinking about how characters are represented (e.g., ASCII codes) and how to manipulate them efficiently.

A brute-force approach would involve iterating through each character and using conditional statements to check and convert as needed. But since this is a common operation, most languages provide optimized built-in methods. The challenge is to understand both approaches and why the built-in method is optimal.

Solution Approach

  • Step 1: Iterate through each character of the input string s.
  • Step 2: For each character, check if it is an uppercase letter (i.e., between 'A' and 'Z').
  • Step 3: If it is uppercase, convert it to the corresponding lowercase letter. This can be done by adding the difference between the ASCII values of 'a' and 'A' to the character.
  • Step 4: If the character is not uppercase, leave it unchanged.
  • Step 5: Combine all processed characters into a new string and return it.

Many programming languages provide a built-in function (like toLowerCase() or lower()) that performs these steps efficiently. Using the built-in method is preferred for clarity and performance, but knowing the manual approach helps understand what happens under the hood.

Design Justification: We use character iteration because strings are immutable in many languages, so creating a new string is necessary. Checking character ranges is efficient because it only involves integer comparisons.

Example Walkthrough

Example Input: s = "Hello, WORLD!"

  • Character 'H' is uppercase → convert to 'h'
  • Character 'e' is lowercase → keep as 'e'
  • Character 'l' is lowercase → keep as 'l'
  • Character 'l' is lowercase → keep as 'l'
  • Character 'o' is lowercase → keep as 'o'
  • Character ',' is not a letter → keep as ','
  • Character ' ' (space) → keep as ' '
  • Character 'W' is uppercase → convert to 'w'
  • Character 'O' is uppercase → convert to 'o'
  • Character 'R' is uppercase → convert to 'r'
  • Character 'L' is uppercase → convert to 'l'
  • Character 'D' is uppercase → convert to 'd'
  • Character '!' is not a letter → keep as '!'

Final Output: "hello, world!"

Time and Space Complexity

  • Brute-force approach: Iterate through each character and manually check/convert if needed. This takes O(n) time, where n is the length of the string, since we process each character once. Space complexity is also O(n) because we create a new string.
  • Optimized (built-in) approach: Using toLowerCase() or lower() is also O(n) time and O(n) space, as the function must process each character and return a new string. The built-in method may be slightly faster due to internal optimizations.

Summary

The "To Lower Case" problem is a straightforward string manipulation task that leverages character-by-character processing. The optimal solution uses a built-in function for clarity and efficiency, but understanding the manual conversion helps build foundational programming skills. The approach is efficient, simple, and elegant, with both time and space complexities linear in the size of the input string.