Convert Uppercase Letters to Lowercase
Solve this ProblemEasy5 min
Topics
BasicsStrings
Companies
TCSInfosysWipro
Given a string
text, return a copy in which every uppercase letter A–Z is replaced by its lowercase version. All other characters stay the same.
It is the mirror of upper-casing — add 32 instead of subtracting it. Converting to one case is called case normalisationCase NormalisationConverting text to a single case so that comparisons and searches ignore capitalisation. and is everywhere in practice: usernames, email addresses and search terms are usually lower-cased before they are compared.
Test Case 1:
Input:text = "Hello, WORLD 2!"
Output:"hello, world 2!"
Explanation:Capitals change; lowercase letters, digits, spaces and punctuation stay.
Test Case 2:
Input:text = "ABC"
Output:"abc"
Explanation:Every letter changes.
Constraints
- ◆
1 ≤ text.length ≤ 200 - ◆
text contains printable ASCII characters
Try the Dry Run
Approach & Solutions
Shift Character Codes by 32Good
The reverse of upper-casing: an uppercase letter's code is 32 less than its lowercase partner. For each character in the range 'A'–'Z', add 32 to its code; leave every other character alone.
Time
O(n)Space
O(n)Java
class Solution {
public String toLower(String text) {
char[] chars = text.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] >= 'A' && chars[i] <= 'Z') {
chars[i] = (char) (chars[i] + 32);
}
}
return new String(chars);
}
}Built-in Lower-Case ConversionOptimal
Use toLowerCase() in Java and JavaScript, lower() in Python, and tolower per character in C and C++. Only letters are affected.
Time
O(n)Space
O(n)Java
class Solution {
public String toLower(String text) {
return text.toLowerCase();
}
}