To Lower Case solution and explanation
9 more videos on this problem
LeetCode Problem: 709. To Lower Case | Java SolutionCode for Interview · 22:07 · 61K views
Leetcode 709. To Lower Case || Code + Explanation Walkthrough || EasyCode with Alisha · 3:14 · 1K views
To Lower Case | LeetCode 709 (Three Methods) | Coding Interview TutorialTerrible Whiteboard · 10:11 · 1K views
Leetcode | 709. To Lower Case | Easy | Java SolutionDeveloper Docs · 1:29 · 835 views
Leetcode 709. To Lower Case | Interview Preparation | English | code iocode io - English · 11:01 · 830 views
To Lower Case | LeetCode 709 | Theory + PythonSai Anish Malla · 7:51 · 677 views
709. To Lower Case | LeetCode | DartAbdul Rahman · 12:34 · 337 views
709. To Lower Case | LeetCode | TypeScriptAbdul Rahman · 14:28 · 275 views
LeetCode 709 : To Lower Case || EASY || C++ ||JAVA || Detailed SolutionCodeGPT · 5:28 · 224 views
To Lower Case - LeetCode Problem #709GuyWho LikesCS · 12:57 · 224 views
The problem
Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.
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.
Approach overview
Problem Overview: Given a string s, convert every uppercase English letter to its lowercase equivalent and return the resulting string. Characters that are already lowercase or not alphabetic remain unchanged. The task focuses on character manipulation within a string while maintaining linear performance.
Approach 1: Use Built-In String Function (O(n) time, O(n) space)
Most languages provide a standard library function such as toLowerCase() or lower(). This method internally iterates through the string and converts each uppercase character to its lowercase counterpart based on the language’s character encoding rules. The implementation is usually optimized in native libraries, so it is both reliable and concise.
You simply call the built-in method on the string and return the result. Internally the operation scans all n characters once, giving O(n) time complexity. A new string is typically created during conversion, which results in O(n) space complexity. For most production code and interviews, this is the cleanest solution because it leverages tested library functionality instead of reimplementing character logic.
Approach 2: Manual ASCII Conversion (O(n) time, O(n) space)
This approach manually converts characters using ASCII values. In ASCII encoding, uppercase letters 'A' to 'Z' map to values 65–90, while lowercase letters 'a' to 'z' map to 97–122. The difference between the uppercase and lowercase version of the same letter is exactly 32.
Iterate through the string character by character. For each character, check if it lies between 'A' and 'Z'. If it does, convert it by adding 32 to its ASCII value (or equivalently shifting to the lowercase range). Append the converted character to a result string; otherwise append the character unchanged. This technique demonstrates understanding of string processing and low-level character operations.
The algorithm performs a single pass through the string, so the time complexity remains O(n). Because most implementations build a new result string or character array, the space complexity is O(n). Some languages allow in-place modification using mutable arrays, but the asymptotic complexity generally stays the same.
Recommended for interviews: Interviewers typically expect the built-in function first because it reflects practical engineering habits. Mentioning the ASCII conversion approach shows deeper understanding of character encoding and how string transformations work internally. Demonstrating both approaches signals that you know when to rely on language libraries and when to implement logic manually.
Approach 1: Use Built-In String Function
This approach utilizes the built-in function available in standard libraries of most programming languages to convert strings to lowercase. Such functions are usually optimized and provide a direct solution to the problem by iterating over each character in the string and converting it to its lowercase equivalent if it's uppercase.
def toLowerCase(s: str) -> str:
return s.lower()
print(toLowerCase("Hello"))class Solution {
public String toLowerCase(String s) {
return s.toLowerCase();
}
public static void main(String[] args) {
Solution solution = new Solution();
String s = "Hello";
System.out.println(solution.toLowerCase(s));
}
}#include <iostream>
#include <algorithm>
std::string toLowerCase(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
return s;
}
int main() {
std::string s = "Hello";
std::cout << toLowerCase(s) << std::endl;
return 0;
}function toLowerCase(s) {
return s.toLowerCase();
}
console.log(toLowerCase('Hello'));#include <stdio.h>
#include <string.h>
#include <ctype.h>
char* toLowerCase(char* s) {
for (int i = 0; s[i] != '\0'; i++) {
s[i] = tolower(s[i]);
}
return s;
}
int main() {
char s[] = "Hello";
printf("%s\n", toLowerCase(s));
return 0;
}using System;
class Solution {
public string ToLowerCase(string s) {
return s.ToLower();
}
static void Main(string[] args) {
Solution solution = new Solution();
string s = "Hello";
Console.WriteLine(solution.ToLowerCase(s));
}
}Python provides the lower() method for strings, which returns a new string in which all uppercase letters have been converted to lowercase.
Time Complexity: O(n), with n being the length of the string.
Space Complexity: O(n), as a new string is created.
Approach 2: Manual ASCII Conversion
This approach manually converts each character by examining its ASCII value. If a character is uppercase (between 65, 'A', and 90, 'Z'), we add 32 to convert it to its lowercase counterpart (between 97, 'a', and 122, 'z'). This method ensures you understand converting characters without relying on the language built-in functions.
def toLowerCase(s: str) -> str:
result = []
for c in s:
if 'A' <= c <= 'Z':
result.append(chr(ord(c) + 32))
else:
result.append(c)
return ''.join(result)
print(toLowerCase("Hello"))class Solution {
public String toLowerCase(String s) {
char[] chars = s.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);
}
public static void main(String[] args) {
Solution solution = new Solution();
String s = "Hello";
System.out.println(solution.toLowerCase(s));
}
}#include <iostream>
std::string toLowerCase(std::string s) {
for (char& c : s) {
if (c >= 'A' && c <= 'Z') {
c += 32;
}
}
return s;
}
int main() {
std::string s = "Hello";
std::cout << toLowerCase(s) << std::endl;
return 0;
}function toLowerCase(s) {
let result = '';
for (let i = 0; i < s.length; i++) {
let ascii = s.charCodeAt(i);
if (ascii >= 65 && ascii <= 90) {
result += String.fromCharCode(ascii + 32);
} else {
result += s[i];
}
}
return result;
}
console.log(toLowerCase('Hello'));#include <stdio.h>
#include <string.h>
char* toLowerCase(char* s) {
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] >= 'A' && s[i] <= 'Z') {
s[i] += 32;
}
}
return s;
}
int main() {
char s[] = "Hello";
printf("%s\n", toLowerCase(s));
return 0;
}using System;
class Solution {
public string ToLowerCase(string s) {
char[] chars = s.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);
}
static void Main(string[] args) {
Solution solution = new Solution();
Console.WriteLine(solution.ToLowerCase("Hello"));
}
}This Python function manually iterates over each character and checks if it's uppercase. If yes, it converts by adding 32 to its ASCII using ord() and chr(). The result is reconstructed into a string.
Time Complexity: O(n), where n represents the string's length.
Space Complexity: O(n), owning to the creation of a new list for the result and a new string.
Approach 3: Default Approach
function toLowerCase(s: string): string {
return s.toLowerCase();
}func toLowerCase(s string) string {
cs := []byte(s)
for i, c := range cs {
if c >= 'A' && c <= 'Z' {
cs[i] |= 32
}
}
return string(cs)
}impl Solution {
pub fn to_lower_case(s: String) -> String {
s.to_ascii_lowercase()
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Use Built-In String Function | Time Complexity: O(n), with n being the length of the string. |
| Manual ASCII Conversion | Time Complexity: O(n), where n represents the string's length. |
| Default Approach | – |
Complexity analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Built-In String Function | O(n) | O(n) | Preferred in production and interviews when standard library utilities are allowed |
| Manual ASCII Conversion | O(n) | O(n) | Useful when implementing character transformations manually or when library functions are restricted |
Frequently asked questions
Is To Lower Case easy or hard?
To Lower Case is classified as an Easy problem. It focuses on basic string traversal and character conversion logic, making it a common introductory exercise for string manipulation.
To Lower Case Python/Java solution
Python uses the built-in method s.lower(), which converts all uppercase letters to lowercase in O(n) time. In Java, the equivalent method is s.toLowerCase(). Both rely on optimized standard library implementations for character transformation.
How to solve To Lower Case in O(n)?
Iterate through the string once and convert uppercase characters to lowercase. Using built-in functions like lower() or toLowerCase() achieves this directly. A manual approach checks if a character lies between 'A' and 'Z' and adds 32 to its ASCII value to convert it.
What is the best approach for To Lower Case?
The built-in string conversion function such as lower() in Python or toLowerCase() in Java is the best approach. It converts every character in a single pass with O(n) time complexity and uses optimized standard library implementations. This approach keeps the code concise and reliable.
Is To Lower Case asked at Google/Amazon/Meta?
To Lower Case is categorized as an easy string manipulation problem and is commonly used for basic screening or warm-up rounds. Similar character processing questions appear in interviews at companies like Amazon, Google, and Meta to test familiarity with string operations.
What data structure is used in To Lower Case?
The problem primarily uses a string or a character array. The algorithm iterates through characters sequentially and either modifies them directly or constructs a new string with converted characters.
What is the time complexity of To Lower Case?
The time complexity is O(n), where n is the length of the string. Each character must be inspected at least once to determine whether it should be converted. Both the built-in method and manual ASCII conversion operate in linear time.
