Sponsored
Sponsored
This approach utilizes the built-in function available in standard libraries of most programming languages to convert strings to lowercase. Such functions are usually optimized and provide a direct solution to the problem by iterating over each character in the string and converting it to its lowercase equivalent if it's uppercase.
Time Complexity: O(n), where n is the length of the string, as we iterate through the string once.
Space Complexity: O(1), as the conversion is done in-place without any additional space.
1def toLowerCase(s: str) -> str:
2 return s.lower()
3
4print(toLowerCase("Hello"))
Python provides the lower()
method for strings, which returns a new string in which all uppercase letters have been converted to lowercase.
This approach manually converts each character by examining its ASCII value. If a character is uppercase (between 65, 'A', and 90, 'Z'), we add 32 to convert it to its lowercase counterpart (between 97, 'a', and 122, 'z'). This method ensures you understand converting characters without relying on the language built-in functions.
Time Complexity: O(n), due to iterating through each character.
Space Complexity: O(1), since the conversion happens in-place.
1
JavaScript's solution manually iterates through each character's ASCII value, adding 32 to convert uppercase to lowercase, constructing the result string character-by-character.