C++ 0709.cpp
class Solution {
public:
string toLowerCase(string s) {
for (auto& c : s)
if (c >= 'A' && c <= 'Z')
c += 'a' - 'A';
return s;
}
};
Difficulty
and has an acceptance rate of about 84% (600k+ submissions).
Typical patterns to solve this problem:
- String
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a Solution class with a single public method toLowerCase. The method receives a mutable copy of a std::string and converts every uppercase ASCII character to its lowercase counterpart, returning the transformed string. The algorithm operates directly on the input string, modifying it in place, which eliminates the need for auxiliary storage.
Core Logic
The heart of the conversion is a range-based for loop that iterates over each character by reference. Inside the loop a conditional checks whether the current character falls within the ASCII range for uppercase letters ('A' to 'Z'). When the condition is true, the character is shifted to its lowercase form by adding the difference between 'a' and 'A'. The relevant fragment is:
for (auto& c : s)
if (c >= 'A' && c <= 'Z')
c += 'a' - 'A';
Because c is bound as a reference (auto&), the modification directly updates the original string buffer. After the loop completes, the method returns the now-lowercased string:
return s;
Edge-Case Handling
The code implicitly handles several edge conditions without additional branches:
- Empty string - The range-based loop simply does not execute, and the empty string is returned unchanged.
- Non-alphabetic characters - Characters outside the
'A'-'Z'range, such as digits, punctuation, or already lowercase letters, bypass the conditional and remain untouched. - Mixed case strings - Only the uppercase portion is transformed; any existing lowercase characters are preserved, yielding a correctly lowercased result for any combination.
- String containing only uppercase letters - Every character satisfies the condition, so the entire buffer is transformed in a single pass.
Performance Characteristics
The algorithm exhibits a tight inner loop with a single conditional and an arithmetic operation. By operating in place, it avoids allocating extra memory, which reduces heap pressure and improves cache locality. The use of a range-based loop with a reference iterator eliminates the overhead of copying characters during iteration. The constant expression 'a' - 'A' is computed at compile time, so the addition inside the loop is a simple integer addition. This combination yields a highly efficient transformation that scales linearly with the length of the input string while maintaining minimal runtime overhead.
ELI5 - Simplified Explanation
Imagine there is a magical box that holds a line of letters. This computer program acts like a friendly helper who walks down the line one letter at a time to check if each one is a big capital letter. If the helper finds a big letter, it gently swaps it for a matching tiny lowercase version. The helper keeps doing this task until every single letter in the line is small and cozy. Once the work is all finished, the line of letters is ready to go.
Code Quality
Complexity Analysis
As a rule of thumb, the maximum input size for one second execution time is
- factorial: up to 10 elements
- exponential: up to 20 elements
- polynomial: a few thousand elements
- loglinear: up to 100,000 elements
- linear: a few million elements
- logarithmic: a billion elements
- constant: practically unlimited
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| author | solutions | ||||||||||
| www | AlgoMonster | Python | Java | C++ | TS | ||||||
| WalkCCC | Python | Java | C++ | ||||||||
| leetcode.ca | Python | Java | C++ | Go | C | Rust | TS | ||||
| LeetcodeHelp | Python | Java | C++ | ||||||||
| Progiez | Python | Java | C++ | ||||||||
| github | BigEggStudy | C# | |||||||||
| black-shadows | C++ | ||||||||||
| chandansgowda | Python | ||||||||||
| charles-wangkai | Java | ||||||||||
| DimitrisJim | C | ||||||||||
| fartem | Ruby | ||||||||||
| halfrost | Go | ||||||||||
| JoshCrozier | JS | ||||||||||
| keineahnung2345 | C++ | ||||||||||
| Krazune | C | ||||||||||
| lkwq007 | Python | ||||||||||
| muhammadfarhankt | Go | ||||||||||
| qiyuangong | Java | ||||||||||
| ruoru | Rust | ||||||||||
| suxrobGM | C# | ||||||||||
| varunu28 | Java | ||||||||||
| YuriSpiridonov | Python | ||||||||||
| AlgoMonster | C++, Python, Java, TypeScript | ||||||||||
| WalkCCC | C++, Python, Java | ||||||||||
| leetcode.ca | C, C++, Go, Java, Python, Rust, TypeScript | ||||||||||
| LeetcodeHelp | C++, Java, Python | ||||||||||
| Progiez | C++, Java, Python | ||||||||||
| BigEggStudy | C# | ||||||||||
| black-shadows | C++, C++ | ||||||||||
| chandansgowda | Python | ||||||||||
| charles-wangkai | Java | ||||||||||
| DimitrisJim | C | ||||||||||
| fartem | Ruby | ||||||||||
| halfrost | Go | ||||||||||
| JoshCrozier | JavaScript | ||||||||||
| keineahnung2345 | C++ | ||||||||||
| Krazune | C | ||||||||||
| lkwq007 | Python | ||||||||||
| muhammadfarhankt | Go | ||||||||||
| qiyuangong | Java | ||||||||||
| ruoru | Rust | ||||||||||
| suxrobGM | C# | ||||||||||
| varunu28 | Java | ||||||||||
| YuriSpiridonov | Python |
Videos
Similar Problems
- problem 2129: Capitalize the Title
Solved On The Same Day
- problem 13: Roman to Integer
- problem 21: Merge Two Sorted Lists
- problem 28: Find the Index of the First Occurrence in a String
- problem 35: Search Insert Position
- problem 58: Length of Last Word
- problem 66: Plus One
- problem 67: Add Binary
- problem 69: Sqrt(x)
- problem 70: Climbing Stairs
- problem 206: Reverse Linked List
- problem 242: Valid Anagram
- problem 278: First Bad Version
- problem 283: Move Zeroes
- problem 367: Valid Perfect Square
- problem 374: Guess Number Higher or Lower
- problem 389: Find the Difference
- problem 441: Arranging Coins
- problem 459: Repeated Substring Pattern
- problem 509: Fibonacci Number
- problem 657: Robot Return to Origin
- problem 682: Baseball Game
- problem 704: Binary Search
- problem 744: Find Smallest Letter Greater Than Target
- problem 746: Min Cost Climbing Stairs
- problem 860: Lemonade Change
- problem 896: Monotonic Array
- problem 976: Largest Perimeter Triangle
- problem 1137: N-th Tribonacci Number
- problem 1232: Check If It Is a Straight Line
- problem 1275: Find Winner on a Tic Tac Toe Game
- problem 1351: Count Negative Numbers in a Sorted Matrix
- problem 1491: Average Salary Excluding the Minimum and Maximum Salary
- problem 1502: Can Make Arithmetic Progression From Sequence
- problem 1523: Count Odd Numbers in an Interval Range
- problem 1539: Kth Missing Positive Number
- problem 1572: Matrix Diagonal Sum
- problem 1672: Richest Customer Wealth
- problem 1768: Merge Strings Alternately
- problem 1822: Sign of the Product of an Array
- problem 2: Add Two Numbers
- problem 5: Longest Palindromic Substring
- problem 33: Search in Rotated Sorted Array
- problem 34: Find First and Last Position of Element in Sorted Array
- problem 43: Multiply Strings
- problem 50: Pow(x, n)
- problem 54: Spiral Matrix
- problem 62: Unique Paths
- problem 63: Unique Paths II
- problem 64: Minimum Path Sum
- problem 73: Set Matrix Zeroes
- problem 74: Search a 2D Matrix
- problem 120: Triangle
- problem 139: Word Break
- problem 153: Find Minimum in Rotated Sorted Array
- problem 198: House Robber
- problem 221: Maximal Square
- problem 275: H-Index II
- problem 279: Perfect Squares
- problem 300: Longest Increasing Subsequence
- problem 436: Find Right Interval
- problem 445: Add Two Numbers II
- problem 528: Random Pick with Weight
- problem 540: Single Element in a Sorted Array
- problem 611: Valid Triangle Number
- problem 658: Find K Closest Elements
- problem 852: Peak Index in a Mountain Array
- problem 981: Time Based Key-Value Store
- problem 1041: Robot Bounded In Circle
- problem 1146: Snapshot Array
- problem 2300: Successful Pairs of Spells and Potions
- problem 154: Find Minimum in Rotated Sorted Array II
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality