Leetcode 709. To Lower Case

Problem Explanation

The task requires us to implement a function which turns all the uppercase letters in a string into lowercase.

For example:

  • If the input is "Hello", the output would be "hello".
  • If the input is "here", the output would be "here".
  • If the input is "LOVELY", the output would be "lovely".

Solution Approach

The solution makes use of the fact that ASCII values of uppercase and lowercase letters have a certain pattern. Uppercase characters have ASCII values from 65 to 90 while lowercase characters have ASCII values from 97 to 122. The difference between ASCII values of an uppsercase letter and its lowercase counterpart is exactly 32. For instance, if 'A' has an ASCII value of 65, 'a' has ASCII value of 97. So, subtracting the ASCII value of 'A' from those of 'a' gives us 32.

In the given solution, we iterate over each character in the string. If the character is an uppercase letter(A-Z), we convert it to lowercase(a-z) by subtracting the difference (which is 32) from the ASCII value of the character.

Python Solution

1
2python
3class Solution:
4  def toLowerCase(self, str: str) -> str:
5    # The difference between an uppercase and its corresponding lowercase letter
6    diff = ord('A') - ord('a')
7      
8    # Convert string to list since strings are immutable in python
9    str = list(str)
10      
11    # Iterate over each character in the string
12    for i in range(len(str)):
13      # Check if the character is an uppercase letter
14      if str[i] >= 'A' and str[i] <= 'Z':
15        # Change the letter to lowercase by subtracting the difference from its ASCII value
16        str[i] = chr(ord(str[i]) - diff)
17
18    return "".join(str)

Java Solution

1
2java
3class Solution {
4  public String toLowerCase(String str) {
5    // The difference between an uppercase and its corresponding lowercase letter
6    int diff = 'A' - 'a';
7
8    // Convert string to char array
9    char[] strArray = str.toCharArray();
10
11    // Iterate over each character in the string
12    for (int i = 0; i < strArray.length; i++) {
13      // Check if the character is an uppercase letter
14      if (strArray[i] >= 'A' && strArray[i] <= 'Z') {
15        // Change the letter to lowercase by subtracting the difference from its ASCII value
16        strArray[i] = (char)(strArray[i] - diff);
17      }
18    }
19    
20    return new String(strArray);
21  }
22}

JavaScript Solution

1
2javascript
3class Solution {
4  toLowerCase(str) {
5    // The difference between an uppercase and its corresponding lowercase letter
6    const diff = 'A'.charCodeAt(0) - 'a'.charCodeAt(0);
7
8    // Convert string to array
9    let strArray = str.split("");
10
11    // Iterate over each character in the string
12    for (let i = 0; i < strArray.length; i++) {
13      // Check if the character is an uppercase letter
14      if (strArray[i] >= 'A' && strArray[i] <= 'Z') {
15        // Change the letter to lowercase by subtracting the difference from its ASCII value
16        strArray[i] = String.fromCharCode(strArray[i].charCodeAt(0) - diff);
17      }
18    }
19
20    return strArray.join("");
21  }
22}

C++ Solution

1
2cpp
3class Solution {
4public:
5  string toLowerCase(string str) {
6    // The difference between an uppercase and its corresponding lowercase letter
7    const int diff = 'A' - 'a';
8
9    // Iterate over each character in the string
10    for (char& c : str)
11      // Check if the character is an uppercase letter
12      if (c >= 'A' && c <= 'Z')
13        // Change the letter to lowercase by subtracting the difference from its ASCII value
14        c -= diff;
15
16    return str;
17  }
18};

C# Solution

1
2csharp
3public class Solution {
4    public string ToLowerCase(string str) {
5        // The difference between an uppercase and its corresponding lowercase letter
6        int diff = 'A' - 'a';
7
8        // Convert string to char array
9        char[] strArray = str.ToCharArray();
10
11        // Iterate over each character in the string
12        for (int i = 0; i < str.Length; i++) {
13            // Check if the character is an uppercase letter
14            if (strArray[i] >= 'A' && strArray[i] <= 'Z') {
15                // Change the letter to lowercase by subtracting the difference from its ASCII value
16                strArray[i] = (char)(strArray[i] - diff);
17            }
18        }
19        
20        return new string(strArray);
21    }
22}

Summary

In this article, we explained how to solve a problem of converting uppercase letters to lowercase in a string by considering the ASCII values of characters. The key to solving this problem lies in understanding the numeric difference between uppercase and lowercase ASCII values. This procedure is then applied in various popular programming languages - Python, Java, JavaScript, C++ and C#. All the solutions follow similar logic to iterate over the characters in the string and convert uppercase characters to lowercase. The operations may vary slightly from one language to another due to their inbuilt functions and datatypes behavior.

However, the approach remains the same. Given that strings are immutable in some languages like Python, we might need to convert them to mutable datatypes like lists for in-place operations. Alternatively, new strings or character arrays could be made to store the modified strings. The difficulty level of this problem is generally considered easy. Understanding the ASCII values and the character manipulation on them is the key learning outcome from this problem.


Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.


TA 👨‍🏫