#709
To Lower Case
EasyStringString ManipulationCharacter Encoding
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n) |
| Space | O(1) | O(1) |
💡
Intuition
Time O(n)Space O(1)
The optimal solution leverages built-in functions for character conversion, which are efficient and straightforward. This approach minimizes manual checks and utilizes the language's capabilities to handle string manipulation effectively.
⚙️
Algorithm
2 steps- 1Step 1: Use the built-in lower() function to convert the entire string to lowercase.
- 2Step 2: Return the converted string.
solution.py2 lines
1def to_lower_case(s):
2 return s.lower()ℹ
Complexity note: The time complexity is O(n) because we traverse the string once to convert it. The space complexity is O(1) since we are modifying the string in place without using additional space.
- 1Understanding ASCII values helps in character manipulation.
- 2Using built-in functions can significantly reduce code complexity.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.