Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.
Example 1:
Input: s = "Hello" Output: "hello"
Example 2:
Input: s = "here" Output: "here"
Example 3:
Input: s = "LOVELY" Output: "lovely"
Constraints:
1 <= s.length <= 100s consists of printable ASCII characters.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.
In this C example, the tolower() function from ctype.h is used. It converts each character of the string to lowercase in-place if it is an uppercase letter.
C++
Java
Python
C#
JavaScript
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.
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.
This solution iterates over the string and checks each character. If it's an uppercase letter, it adds 32 (difference in ASCII values between uppercase and lowercase letters) to convert it to lowercase.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), due to iterating through each character.
Space Complexity: O(1), since the conversion happens in-place.
| Approach | Complexity |
|---|---|
| Use Built-In String Function | Time Complexity: O(n), where n is the length of the string, as we iterate through the string once. |
| Manual ASCII Conversion | Time Complexity: O(n), due to iterating through each character. |
LeetCode To Lower Case Solution Explained - Java • Nick White • 21,922 views views
Watch 9 more video solutions →Practice To Lower Case with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor