algoadvance

Leetcode 709. To Lower Case

Problem Statement

Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.

Clarifying Questions

  1. Is the input string restricted to just alphabetic characters?
    • No, the input string can contain any printable characters.
  2. Can the input string be empty?
    • Yes, the input string can be empty, in which case the output should also be an empty string.
  3. Do we need to handle non-English alphabet characters?
    • The problem typically deals with English alphabet characters only, but it’s good practice to ensure the solution is robust for any UTF-8 characters.
  4. Are there any constraints on the length of the input string?
    • No specific constraints, but typically it should be expected to handle reasonably large strings within the usual limits of the problem context.

Strategy

Java provides built-in methods to convert a string to lowercase, making this problem straightforward to solve using the toLowerCase method of the String class.

Code

public class Solution {
    public String toLowerCase(String s) {
        return s.toLowerCase();
    }
    
    public static void main(String[] args) {
        Solution solution = new Solution();
        // Test Cases
        System.out.println(solution.toLowerCase("Hello")); // Output: "hello"
        System.out.println(solution.toLowerCase("here")); // Output: "here"
        System.out.println(solution.toLowerCase("LOVELY")); // Output: "lovely"
    }
}

Time Complexity

Using the toLowerCase method leverages Java’s highly optimized internal libraries for character conversion, providing a concise and efficient solution.

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI