In this article, we will be solving the Leetcode problem 709 – “To Lower Case,” which is a simple yet important problem that deals with string manipulation. In this problem, we are asked to implement a function to convert a string to lowercase.
While this might seem trivial at first, and indeed Python provides built-in functions for this purpose, the goal is to understand the underpinnings of these operations and implement them from scratch.
Problem Statement
Here is the exact problem statement from Leetcode:
Implement function toLowerCase()
that has a string parameter str
, and returns the same string in lowercase.
Example 1:
Input: "Hello"
Output: "hello"
Example 2:
Input: "here"
Output: "here"
Example 3:
Input: "LOVELY"
Output: "lovely"
As the problem states, we are given a string as input, and our task is to return the string in lowercase.
Solution Using Built-In Python Function
The most straightforward way to solve this problem is to use Python’s built-in function lower()
. This function returns a copy of the original string converted to lowercase.
class Solution:
def toLowerCase(self, str: str) -> str:
return str.lower()
This solution works perfectly fine and is indeed the most Pythonic way of solving this problem. However, it doesn’t provide much insight into how we could solve this problem from scratch.
Solution Using ASCII Values
ASCII (American Standard Code for Information Interchange) is a character encoding standard used to represent characters on electronic devices. Each character corresponds to a unique number from 0 to 127.
In the ASCII table, the difference between the uppercase and lowercase letters is 32. That is, the ASCII value of ‘A’ is 65, and the ASCII value of ‘a’ is 97, a difference of 32. This pattern holds for all alphabetic characters.
Knowing this, we can write a function to convert each uppercase letter in the string to a lowercase letter.
class Solution:
def toLowerCase(self, str: str) -> str:
return ''.join(chr(ord(c) + 32) if 'A' <= c <= 'Z' else c for c in str)
In this solution, we use the built-in Python functions ord()
and chr()
. The ord()
function returns the ASCII value of a character, and chr()
does the reverse, taking an ASCII value and returning the corresponding character.
We iterate over the input string, and for each character, we check if it’s an uppercase letter. If it is, we add 32 to its ASCII value to convert it to lowercase and use chr()
to convert the new ASCII value back to a character.
Solution Using Python Dictionary
Another way to solve this problem is by using a Python dictionary to map uppercase letters to lowercase letters.
class Solution:
def toLowerCase(self, str: str) -> str:
upper_to_lower = {chr(i): chr(i + 32) for i in range(ord('A'), ord('Z')+1)}
return ''.join(upper_to_lower[c] if c in upper_to_lower else c for c in str)
In this solution, we first create a dictionary that maps uppercase letters to lowercase letters. Then, we iterate over the string and replace each uppercase letter with its lowercase equivalent using the dictionary.
Conclusion
This article walked you through different methods of solving the Leetcode problem “To Lower Case,” which required converting a string to lowercase. We started with a simple solution using Python’s built-in lower()
function, then we moved on to a slightly more complex solution using ASCII values, and finally, we discussed a solution using a Python dictionary.