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.The goal of #709 To Lower Case is to convert all uppercase letters in a string to their lowercase equivalents. Since the input is a simple string, the most straightforward idea is to traverse the string character by character and transform only the characters that fall within the uppercase alphabet range.
One common approach is to check if a character lies between 'A' and 'Z'. If it does, it can be converted to lowercase by applying the known ASCII offset between uppercase and lowercase letters. Another practical option is to use built-in string utilities available in most programming languages, which internally perform the same conversion efficiently.
The key insight is that every character is processed exactly once, making the approach highly efficient. This results in a linear scan of the string with minimal additional memory usage, making it suitable even for longer inputs.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Character-by-character traversal with ASCII conversion | O(n) | O(1) |
| Using built-in lowercase string function | O(n) | O(1) |
Nick White
Use these hints if you're stuck. Try solving on your own first.
Most languages support lowercase conversion for a string data type. However, that is certainly not the purpose of the problem. Think about how the implementation of the lowercase function call can be done easily.
<b>Think ASCII!</b>
Think about the different capital letters and their ASCII codes and how that relates to their lowercase counterparts. Does there seem to be any pattern there? Any mathematical relationship that we can use?
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.
1#include <stdio.h>
2#include <string.h>
3#include <ctype.h>
4
5char* toLowerCase(char* s) {
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.
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#
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Problems like To Lower Case are typically used as warm-up or screening questions in technical interviews. They test familiarity with string manipulation, ASCII values, and basic iteration logic.
In ASCII, uppercase and lowercase letters have a fixed numeric difference. By checking if a character falls between 'A' and 'Z' and applying the offset, it can be converted to the corresponding lowercase character efficiently.
A simple string or character array is sufficient for this problem. Since the task only requires sequential traversal and modification of characters, no complex data structures like hash maps or stacks are needed.
The optimal approach is to iterate through the string and convert uppercase letters to lowercase during traversal. This can be done either with ASCII arithmetic or by using built-in string functions. Both approaches run in linear time because each character is processed once.
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.