Back to blog
Jun 30, 2025
3 min read

To Lower Case

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

Difficulty: Easy | Acceptance: 84.80% | Paid: No Topics: String

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

Examples

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 <= 100
s consists of printable ASCII characters.

Approach 1: Built-in Functions

Intuition Most programming languages provide standard library methods to convert strings to lowercase, which handles the logic internally.

Steps

  • Call the language-specific lowercase method on the input string.
  • Return the result.
python
class Solution:
    def toLowerCase(self, s: str) -&gt; str:
        return s.lower()

Complexity

  • Time: O(n)
  • Space: O(n)
  • Notes: Strings are immutable in many languages, so a new string is allocated.

Approach 2: ASCII Manipulation

Intuition Uppercase letters ‘A’ through ‘Z’ have ASCII values from 65 to 90, while lowercase ‘a’ through ‘z’ range from 97 to 122. The difference is 32. We can iterate through the string and add 32 to any character that falls within the uppercase range.

Steps

  • Initialize an empty result string or builder.
  • Iterate through each character in the input string.
  • If the character’s ASCII value is between 65 (‘A’) and 90 (‘Z’), add 32 to it to get the lowercase version.
  • Append the character (converted or original) to the result.
  • Return the result.
python
class Solution:
    def toLowerCase(self, s: str) -&gt; str:
        res = []
        for ch in s:
            if 'A' &lt;= ch &lt;= 'Z':
                res.append(chr(ord(ch) + 32))
            else:
                res.append(ch)
        return ''.join(res)

Complexity

  • Time: O(n)
  • Space: O(n)
  • Notes: This approach avoids the overhead of function calls found in built-in methods.

Approach 3: Lookup Table

Intuition We can precompute a mapping of all 128 ASCII characters to their lowercase equivalents. This allows for O(1) lookup per character during iteration.

Steps

  • Create an array or map of size 128 (for standard ASCII).
  • Populate the array such that index ‘A’ maps to ‘a’, ‘B’ to ‘b’, etc., and all other characters map to themselves.
  • Iterate through the input string, looking up each character in the table to build the result.
python
class Solution:
    def toLowerCase(self, s: str) -&gt; str:
        mapping = {chr(i): chr(i + 32) for i in range(65, 91)}
        return ''.join(mapping.get(ch, ch) for ch in s)

Complexity

  • Time: O(n)
  • Space: O(1) (The lookup table is fixed size 128, regardless of input n)
  • Notes: Useful if the conversion logic is more complex than a simple offset.