LeetCode 709. To Lower Case

Description

https://leetcode.com/problems/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"

Explanation

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'))
            else:
                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 *