Today I am going to talk about how I have implemented the lowercase problem from LeetCode. To have all the information in one place and to avoid moving from one page to another, I have copied the problem below.
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"
Let’s start solving this problem. As we know, Strings are immutable in many programming languages, and Python is no exception. Thus, to change the string passed as a parameter, I have created an empty string (str2 = ‘ ‘) that will represent the new String to lowercase.
The next step is to loop over the original String (str). len(str) returns the length of the String. For example, the String “hello” has the length five. Thus, the loop runs five times, and each time the variable ‘x’ is assigned the number of times it ran up to that point. If it is on its fourth iteration, the variable ‘x’ is equal to four, and ‘ch’ is assigned the character ‘o’ from hello, for instance. Do not forget that it starts from 0, so str[x] becomes str, str, str, str, str.
for x in range(len(str)): ch = str[x]
Another way of doing this would be:
for x in str: .....
I prefer the first approach, and I have also got better runtime numbers with the first one.
The next step is to check each character from the String whether it is an uppercase or a lowercase letter. To do that, I made use of the ASCII table which specifies the numerical representation of a character. For example, the letter ‘A’ has a numerical representation of 65, whereas the letter ‘a’ has a numerical representation of 97. However, to get the numerical representation of a character I made use of the ord() function from the Python’s standard library. Once I have this information, I take each character and check if it is between the numbers 64 and 91.
if ord(ch) > 64 and ord(ch) < 91:
The reason for using the numbers 64 and 91 is that the uppercase letters start from 65 (which is uppercase A) and it finishes at 90 (which is uppercase Z). Therefore, if the numerical representation of the ‘ch’ is between these two numbers, it means the character is in uppercase. If it is in uppercase, we need to convert it to lowercase and append it to the variable str2.
ch = chr(ord(ch) + 32)
Another useful method is the ‘chr()‘ method. This method takes the numerical representation, and it returns the corresponding character. In our code, it takes the original representation of ‘ch‘ (ord(ch)) and adds 32 to that number. Why 32? If you look above, I have said that uppercase letters start at 65 – A and finish at 90 – Z. On the other side, lowercase letters start at 97 – a and end at 122 – z. Now if you do the maths, you will see that 65 + 32 returns 97, which is lowercase a. That means, there is always a difference of 32 between uppercase and lowercase letters. If you know the numerical code of an uppercase letter, you can add 32 to get the lowercase of the same letter. If you know the numerical code of a lowercase letter you do the opposite; you subtract 32 from its value.
Then, append the newly converted letter to the new String (str2).
str2 += ch
All that is left is the else statement. If the letter is already in lowercase, append the letter to the variable str2. Once the iteration is over, and all the characters are checked, return the new String.
else: str2 += ch return str2
The full solution to the problem is as follows:
class Solution(object): def toLowerCase(self, str): """ :type str: str :rtype: str """ str2 = '' for x in range(len(str)): ch = str[x] # check if the character is an uppercase character if ord(ch) > 64 and ord(ch) < 91: # if so, make it a lowercase character and append it to str2 ch = chr(ord(ch) + 32) str2 += ch else: str2 += ch return str2
There is an even shorter answer to this problem:
class Solution(object): def toLowerCase(self, str): """ :type str: str :rtype: str """ return str.lower()
However, the point of the problem is to think through and to implement your method of lower().
When it comes to efficiency, it is not the best solution, and it can be optimized further. I ran the method twelve times, and it took 34.66 ms on average.
DISCLAIMER: I am a beginner, I am studying Data Structures and Algorithms in my own time, and my solutions are not the best ones. I use this blog as a notebook for myself where I store my answers to various programming challenges. The purpose of this blog is to practice and to make sure I understand what I have done by explaining the steps I have taken to solve the challenge. If you have better a solution leave a comment.