742. To Lower Case
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to convert a given string s to lowercase. This means every uppercase English letter in the string should be replaced by its corresponding lowercase letter, while all other characters should remain unchanged. The hints suggest that we should implement this conversion manually, likely by leveraging the properties of character encodings like ASCII.
Solution
To Lower Case in Python
class Solution:
def toLowerCase(self, s: str) -> str:
# Python strings are immutable. We build a list of characters
# and then join them to form the new string.
result_chars = []
# The difference between ASCII values of lowercase and uppercase letters.
# ord() gets the ASCII value of a character.
offset = ord('a') - ord('A')
# Iterate through each character in the input string.
for char in s:
# Check if the character is an uppercase letter.
if 'A' <= char <= 'Z':
# Convert to lowercase by adding the offset to its ASCII value
# and then converting it back to a character with chr().
lower_char = chr(ord(char) + offset)
result_chars.append(lower_char)
else:
# If not an uppercase letter, append it as is.
result_chars.append(char)
# Join the characters in the list to form the final string.
return "".join(result_chars)
To Lower Case in Java
class Solution {
public String toLowerCase(String s) {
// Strings are immutable in Java. Using a char array is efficient.
char[] charArray = s.toCharArray();
// Iterate through the character array.
for (int i = 0; i < charArray.length; i++) {
char c = charArray[i];
// Check if the character is an uppercase letter.
if (c >= 'A' && c <= 'Z') {
// Convert to lowercase by adding the offset.
// The cast to char is needed because arithmetic
// on char promotes the result to int.
charArray[i] = (char) (c + ('a' - 'A'));
}
}
// Create a new string from the modified character array.
return new String(charArray);
}
}
To Lower Case in C++
class Solution {
public:
string toLowerCase(string s) {
// Iterate through each character of the string by reference.
for (char &c : s) {
// Check if the character is an uppercase letter ('A' through 'Z').
if (c >= 'A' && c <= 'Z') {
// Convert it to lowercase by adding the difference
// between 'a' and 'A'. This is more portable than
// adding a hardcoded 32, as it relies on the
// character set's properties rather than specific ASCII values.
c = c + ('a' - 'A');
}
}
// C++ strings are mutable, so we can return the modified string.
return s;
}
};
To Lower Case in TypeScript
function toLowerCase(s: string): string {
// In TypeScript/JavaScript, strings are immutable.
// We build an array of characters and then join them.
const resultChars: string[] = [];
const offset = 'a'.charCodeAt(0) - 'A'.charCodeAt(0);
// Iterate through each character of the string.
for (const char of s) {
// Check if the character is an uppercase letter.
if (char >= 'A' && char <= 'Z') {
// Get its char code, add the offset, and convert back to a character.
const lowerCharCode = char.charCodeAt(0) + offset;
resultChars.push(String.fromCharCode(lowerCharCode));
} else {
// If not an uppercase letter, push the original character.
resultChars.push(char);
}
}
// Join the array of characters into a single string.
return resultChars.join('');
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.