To Lower Case
EasyUpdated: Aug 2, 2025
Practice on:
Problem
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 <= 100sconsists 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
C++
#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;
}
};
Java
class Solution {
public String toLowerCase(String s) {
return s.toLowerCase();
}
}
Python
class Solution:
def toLowerCase(self, s: str) -> str:
return s.lower()
Rust
impl Solution {
pub fn to_lower_case(s: String) -> String {
s.to_ascii_lowercase()
}
}
TypeScript
function toLowerCase(s: string): string {
return s.toLowerCase();
}
Complexity
- ⏰ Time complexity:
O(n) - 🧺 Space complexity:
O(n)