Problem

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

Examples

Example 1

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

Example 2

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

Example 3

1
2
Input: s = "LOVELY"
Output: "lovely"

Constraints

  • 1 <= s.length <= 100
  • s consists of printable ASCII characters.

Solution

Method 1 – Built-in String Conversion

Intuition

Use the language’s built-in string lowercasing function for a direct and efficient solution.

Approach

Call the standard library’s toLowerCase/lower() method on the input string.

Code

1
2
3
4
5
6
7
8
9
#include <string>
using namespace std;
class Solution {
public:
    string toLowerCase(string s) {
        for (char& c : s) if (c >= 'A' && c <= 'Z') c = c - 'A' + 'a';
        return s;
    }
};
1
2
3
4
5
class Solution {
    public String toLowerCase(String s) {
        return s.toLowerCase();
    }
}
1
2
3
class Solution:
    def toLowerCase(self, s: str) -> str:
        return s.lower()
1
2
3
4
5
impl Solution {
    pub fn to_lower_case(s: String) -> String {
        s.to_ascii_lowercase()
    }
}
1
2
3
function toLowerCase(s: string): string {
    return s.toLowerCase();
}

Complexity

  • ⏰ Time complexity: O(n)
  • 🧺 Space complexity: O(n)