Similar Problems

Similar Problems not available

To Lower Case - Leetcode Solution

Companies:

LeetCode:  To Lower Case Leetcode Solution

Difficulty: Easy

Topics: string  

Problem Statement:

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"

Solution:

The problem is very simple to solve. One way to solve the problem is to loop through all the characters in the string and convert each uppercase character to lowercase.

We can use the ASCII values of the characters to check if the character is uppercase or lowercase. The ASCII value of 'A' is 65 and the ASCII value of 'Z' is 90. Similarly, the ASCII value of 'a' is 97 and the ASCII value of 'z' is 122.

Therefore, to convert an uppercase character to lowercase, we can add 32 to its ASCII value. For example, the ASCII value of 'A' is 65 and the ASCII value of 'a' is 97. So, to convert 'A' to 'a', we can add 32 to its ASCII value.

We can use String.toCharArray() method to convert the string to a character array and loop through all the characters to replace every uppercase character with a lowercase character.

Code:

public String toLowerCase(String s) { char[] charArray = s.toCharArray(); for(int i=0; i<charArray.length; i++) { if(charArray[i]>='A' && charArray[i]<='Z') { charArray[i] = (char)(charArray[i]+32); } } return new String(charArray); }

Time Complexity:

The algorithm has a time complexity of O(n) where n is the length of the input string s.

Space Complexity:

The algorithm has a space complexity of O(n) where n is the length of the input string s. This is because we are using a character array of length n to store the modified characters.

To Lower Case Solution Code

1