709_To Lower Case

[easy] [string]

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

Example 1:

Input: "Hello"
Output: "hello"

Solution:

Idea:

  • The ASCII code for lowercase and uppercase is different by 32.

Time Complexity: O(n)O(n)

Space Complexity: O(n)O(n)

def toLowerCase(str):
    """
    :type str: str
    :rtype: str
    """

    # use build-in function in python
    # return str.lower()

    # use ASCII code
    out = ''
    for c in str:
        if ord(c) <= 90 and ord(c) >= 65:
            c = chr(ord(c) + 32)
        out += c
    return out

Last updated