LeetCode 709. To Lower Case



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"


Keep track of the existing sum.

Python Solution

class Solution:
    def toLowerCase(self, str: str) -> str:
        result = ""
        for ch in str:
            if ord(ch) > ord('z') or ord(ch) < ord('A'):
                result += ch            
            elif ord(ch) - ord('a') < 0:
                result += chr(ord(ch) - ord('A') + ord('a'))
                result += ch
        return result
  • Time Complexity: O(N).
  • Space Complexity: O(N).

Leave a Reply

Your email address will not be published. Required fields are marked *