Given a string s
, return the string after replacing every uppercase letter with the same lowercase letter.
For example:
s = "Hello"
, the output should be "hello"
.s = "here"
, the output should be "here"
.s = "LOVELY"
, the output should be "lovely"
.Can you provide an efficient algorithm to achieve this? What is the time and space complexity of your solution? Are there any edge cases to consider?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To convert a string to lowercase using brute force, we examine each letter individually. If a letter is uppercase, we change it to its lowercase equivalent; otherwise, we leave it as is.
Here's how the algorithm would work step-by-step:
def to_lower_case_brute_force(input_string):
string_as_list = list(input_string)
for character_index in range(len(string_as_list)):
character = string_as_list[character_index]
# Check if the character is an uppercase letter
if 'A' <= character <= 'Z':
# Calculate the offset from 'A' to convert to lowercase
uppercase_offset = ord(character) - ord('A')
lowercase_character = chr(ord('a') + uppercase_offset)
# Replace the uppercase letter with its lowercase equivalent
string_as_list[character_index] = lowercase_character
return "".join(string_as_list)
We want to change uppercase letters to lowercase. We can achieve this by looking at each letter and, if it's uppercase, changing it based on its position in the alphabet.
Here's how the algorithm would work step-by-step:
def to_lower_case(input_string):
result_string = ''
for char in input_string:
# Check if the character is an uppercase letter
if 'A' <= char <= 'Z':
# Calculate the ASCII value of the lowercase equivalent.
lowercase_char = chr(ord(char) + 32)
result_string += lowercase_char
# If not uppercase, append the original character
else:
result_string += char
return result_string
Case | How to Handle |
---|---|
Null or empty input string | Return an empty string or null depending on the language convention for empty inputs. |
String containing only non-alphabetic characters | The algorithm should correctly skip these characters, leaving them unchanged in the output string. |
String with mixed-case alphabets and non-alphabets | Only upper-case alphabets should be converted and other characters should remain as is. |
String with special characters (e.g., unicode) | Handle unicode characters correctly based on the language's unicode support and conversion functions. |
String with numbers as input | Numbers should be kept as is since toLower() is only for upper case letters. |
Maximum string length (memory constraints) | Ensure the solution avoids memory overflow issues when dealing with very large strings by allocating only the necessary memory. |
Input contains all uppercase characters | The entire input will be converted to lowercase as expected. |
Input contains all lowercase characters | The entire input will remain the same since no changes need to be made. |