709. To Lower Case LeetCode Solution

In this guide, you will get 709. To Lower Case LeetCode Solution with the best time and space complexity. The solution to To Lower Case problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. To Lower Case solution in C++
  4. To Lower Case solution in Java
  5. To Lower Case solution in Python
  6. Additional Resources
709. To Lower Case LeetCode Solution image

Problem Statement of To Lower Case

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 <= 100
s consists of printable ASCII characters.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

709. To Lower Case LeetCode Solution in C++

class Solution {
 public:
  string toLowerCase(string str) {
    const int diff = 'A' - 'a';

    for (char& c : str)
      if (c >= 'A' && c <= 'Z')
        c -= diff;

    return str;
  }
};
/* code provided by PROGIEZ */

709. To Lower Case LeetCode Solution in Java

class Solution {
  public String toLowerCase(String str) {
    final int diff = 'A' - 'a';

    char[] ans = str.toCharArray();

    for (int i = 0; i < ans.length; ++i)
      if (ans[i] >= 'A' && ans[i] <= 'Z')
        ans[i] -= diff;

    return new String(ans);
  }
}
// code provided by PROGIEZ

709. To Lower Case LeetCode Solution in Python

class Solution:
  def toLowerCase(self, str: str) -> str:
    return ''.join(chr(ord(c) + 32) if 'A' <= c <= 'Z' else c for c in str)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.