Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0709 - To Lower Case
    0709 - To Lower Case
    0709 - To Lower Case
    0709 - To Lower Case

    0709 - To Lower Case

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/to-lower-case/

    Solution 1 - Built-in lower()

    Use Python's built-in str.lower() method to convert all uppercase characters in the string to lowercase.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(n)SpaceComplexity: O(n)SpaceComplexity:O(n)

    Solution 2 - ASCII Bit Manipulation

    Iterate through each character. If it is an uppercase letter (A–Z), convert it to lowercase by performing a bitwise OR with 32. This works because the ASCII difference between uppercase and lowercase letters is exactly 32 (bit 5).

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(n)SpaceComplexity: O(n)SpaceComplexity:O(n)
    """ 0709.1 - Solution 1 - Built-in lower() """
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def toLowerCase(self, s: str) -> str:
            """To Lower Case Function"""
            return s.lower()
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().toLowerCase("Hello"))
        print(Solution().toLowerCase("here"))
        print(Solution().toLowerCase("LOVELY"))
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0709.2 - Solution 2 - ASCII Bit Manipulation """
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def toLowerCase(self, s: str) -> str:
            """To Lower Case Function"""
            result = []
            for c in s:
                if 'A' <= c <= 'Z':
                    result.append(chr(ord(c) | 32))
                else:
                    result.append(c)
            return ''.join(result)
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().toLowerCase("Hello"))
        print(Solution().toLowerCase("here"))
        print(Solution().toLowerCase("LOVELY"))
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()