We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

To Lower Case

Number: 742

Difficulty: Easy

Paid? No

Companies: Google


Problem Description

Given a string s, return the string after converting every uppercase letter to the corresponding lowercase letter.


Key Insights

  • The problem requires a simple transformation of each character in the string.
  • Many languages provide built-in functions to convert an entire string to lowercase.
  • The operation can be done in a single pass while maintaining linear time complexity.

Space and Time Complexity

Time Complexity: O(n), where n is the length of the string. Space Complexity: O(n), for storing the new lowercase string (if strings are immutable).


Solution

The solution uses a direct approach by leveraging the built-in lowercase conversion functions available in most programming languages. In languages without a direct function, iterate over the string and convert each character if it is uppercase, then form a new string. This approach is straightforward and optimal given the constraints, and it minimizes error-prone manual character-by-character adjustments.


Code Solutions

# Function to convert string to lowercase
def to_lower_case(s):
    # Use the built-in lower() function to convert the string to lowercase
    return s.lower()

# Example usage:
input_string = "Hello"
print(to_lower_case(input_string))  # Expected output: "hello"
← Back to All Questions