To Lower Case LeetCode Solution - The Coding Shala
Home >> LeetCode >> To Lower Case
Other Posts You May Like
In this post, we will learn how to solve LeetCode's To Lower Case Problem and will implement its solution in Java.
To Lower Case Problem
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Example 1:
Input: "Hello"
Output: "hello"
Practice this problem on LeetCode(Click Here).
To Lower Case Java Solution
Approach 1
We can compare the ASCII value of char and if it is within the capital range then add 32 and change it to char.
32 is the difference between capitals and small char ASCII values.
Java Program:
class Solution { public String toLowerCase(String str) { StringBuilder sb = new StringBuilder(); for(int i=0; i<str.length(); i++) { int ch = (int) str.charAt(i); if(ch >= 65 && ch <= 90) { sb.append((char) (ch+32)); } else sb.append((char) ch); } return sb.toString(); } }
- LeetCode - Buddy Strings
- LeetCode - Crawler Log Folder
- LeetCode - Flipping an Image
- LeetCode - Valid Square
- LeetCode - Number of Steps to Reduce Number to Zero
Comments
Post a Comment