algoadvance

Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.

Example:

  1. Input: s = "Hello" Output: "hello"

  2. Input: s = "here" Output: "here"

  3. Input: s = "LOVELY" Output: "lovely"

Clarifying Questions

  1. Q: What should we return if the string is already all lowercase? A: If the string is already in lowercase, we should return it as is.

  2. Q: Are there any constraints on the character set of the string? Is it always composed of alphabetic characters? A: The problem implies dealing with alphabetic characters, but in typical problems of this kind, other characters should be left unchanged.

  3. Q: Is the input string s guaranteed to be non-empty? A: The prompt does not specify, so we should handle possible edge cases, including an empty string.

Strategy

Python provides an efficient method to convert a string to lowercase: str.lower(). This built-in method is optimal and will handle converting each uppercase letter to its corresponding lowercase equivalent, leaving other characters (such as digits and punctuation) unchanged.

The detailed steps are:

  1. Utilize Python’s built-in lower() method on the input string s.
  2. Return the resultant lowercase string.

This method is efficient and straightforward.

Code

Here’s the implementation:

def toLowerCase(s: str) -> str:
    return s.lower()

Time Complexity

The time complexity of this implementation is O(n), where n is the length of the string s. The lower() method processes each character exactly once, making it ideally efficient for this problem.

Usage

This solution is direct and leverages Python’s optimized string methods to ensure both readability and performance.

Try our interview co-pilot at AlgoAdvance.com