Convert Uppercase Letters to Lowercase

Solve this Problem
Easy5 min
Topics
Companies
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.

TimeO(n)
SpaceO(n)
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.

TimeO(n)
SpaceO(n)
class Solution { public String toLower(String text) { return text.toLowerCase(); } }

Related Problems