To Lower Case Leetcode Solution

Difficulty Level Easy
Frequently asked in Adobe Apple Google
algorithms coding Interview interviewprep LeetCode LeetCodeSolutions StringViews 4501

The problem To Lower Case Leetcode Solution provides us with a string and asks us to convert all the upper case alphabets into lower case alphabets. We are required to convert all the upper case or lower case alphabets into lower case characters. So, the problem seems simple but before diving into the solution. We take a look at a few examples.

To Lower Case Leetcode Solution

"Hello"
"hello"

Explanation: The input has an upper case alphabet ‘H’ which is converted into the lower-case alphabet ‘h’. The other characters “ello” stay the same and we do not need to convert them.

"here"
"here"

Explanation: Since all the alphabets in the input are lower case characters. We do not need to change any of the characters and can return the input without any modification.

Approach using in-built functions

The problem To Lower Case Leetcode Solution asked us to simply convert the upper case character to the lower case character. This operation can be easily done using in-built functions in programming languages. So, we can use tolower() in C++, or toLowerCase() in Java. Using these functions, we only need to pass the given string as input. Then we get the string with all characters in lower case.

Code

C++ code for To Lower Case Leetcode Solution

#include <bits/stdc++.h>
using namespace std;

string toLowerCase(string str) {
    transform(str.begin(), str.end(), str.begin(), [](unsigned char c){ return std::tolower(c); });
    return str;
}

int main(){
    cout<<toLowerCase("Hello");
}
hello

Java code for To Lower Case Leetcode Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class Main {
    public static String toLowerCase(String str) {
        return str.toLowerCase();
    }
    
    public static void main (String[] args) throws java.lang.Exception
  {
      System.out.print(toLowerCase("Hello"));
  }
}
hello

Complexity Analysis

Time Complexity

O(N), the in-built methods also traverse the whole input string to convert the given string to lower case characters.

Space Complexity

O(1), since we do not need to store anything. The space complexity is constant.

Alternative Approach for To Lower Case Leetcode Solution

The alternative approach that we can utilize, is ASCII numbers. We can simply create a function that traverses over the given input string and checks if the ASCII code of the character lies between the ASCII code of A to Z. We simply add the difference between the ASCII code a and A. This will change the character to its corresponding lower case character. But strings are immutable in java so we need StringBuffer to complete the task.

Code for Alternative Approach

C++ code for To Lower Case Leetcode Solution

#include <bits/stdc++.h>
using namespace std;

string toLowerCase(string str) {
    for(int i=0;i<str.length();i++)
        if(str[i]>='A' && str[i]<='Z')
            str[i] = str[i] + ('a' - 'A');
    return str;
}

int main(){
    cout<<toLowerCase("Hello");
}
hello

Java code for To Lower Case Leetcode Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class Main {
    public static String toLowerCase(String str) {
    	StringBuffer res = new StringBuffer();
        for(int i=0;i<str.length();i++)
        	if(str.charAt(i)>='A' && str.charAt(i)<='Z')
            	res.append((char)(str.charAt(i) + ('a' - 'A')));
            else
            	res.append(str.charAt(i));
    	return res.toString();
    }
    
    public static void main (String[] args) throws java.lang.Exception
  {
      System.out.print(toLowerCase("Hello"));
  }
}
hello

Complexity Analysis

Time Complexity

O(N), where N is the size of the given string. Since we need to traverse the whole;e string, the time complexity is dependent on the size of the input.

Space Complexity

O(1) in C++, because the operation is in-place. The space complexity of the entire algorithm is constant. But it takes O(N) in Java because strings are immutable in java and we need to create a new string to store the result.

Translate »