Capitalize The Title LeetCode

Problem Link:


https://leetcode.com/problems/capitalize-the-title/




Solution: 


Using ArrayList:


 String arr[];

            arr=s.split(" ");

        ArrayList<String>aa=new ArrayList<String>();

      for(int i=0;i<arr.length;i++){

          aa.add(arr[i]);

      }

        

        for(int i=0;i<aa.size();i++){

            aa.set(i,aa.get(i).toLowerCase());

            if(aa.get(i).length()>2){

                String a=Character.toUpperCase(aa.get(i).charAt(0)) + aa.get(i).substring(1);

                aa.set(i,a);

            }

        }

        StringBuilder sb=new StringBuilder();

        for(int i=0;i<aa.size();i++){

            sb.append(aa.get(i));

            sb.append(" ");

        }

        sb.deleteCharAt(sb.length()-1);

        return sb.toString();


TIME COMPLEXITY: O(String Length)[LeetCode Time:16 ms faster than 26.16%]

SPACE COMPLEXITY: O(Total Words In String)[It is a must for us to use a string array to extract all the words present in the string. We have to make one array for that and that is a must.]

AUXILLARY SPACE: O(Words in String)[Memory Used: 43.8MB less than 4.78%]

TOTAL TEST CASES:200

APPROACH:

Here first we will extract all the words present in the string using the split() function and store them in the array.

Now we will further add each string into ArrayList for our convenience of applying string functions and manipulating.

Now we will traverse the ArrayList and then take each element specifically ie String and convert it to lower case using the toLowerCase() function.

(Here in question they have said that weather string size is less than 2 it will be in lower case or if it is more than 2 then also except the first letter whole string will be in lower case.)

Now we will see the size of the string whether it is more than 2 or not.

If it is more than 2 then we will modify the first character to upper case using Character.toUpperCase() function and then using substring function we fix all the characters in lower case.

Later we use the StringBuilder object and append the string with " ".

Question Why do we use the delete function at the end of StringBuilder?

Answer: This is so because earlier we append all the strings present in ArrayList with " " into StringBuilder object so by doing this after last addition also " " will be appended there in StringBuilder.So to remove it we have used deleteCharAt(last index) function.

Later return sb.toString().  




=========================================================================

Solution 2:

Without Using ArrayList:


 String arr[];

            arr=s.split(" ");

        

        int index=0;

        

        for(String a:arr){

            a=a.toLowerCase();

            if(a.length()>2){

                

                arr[index]=Character.toUpperCase(a.charAt(0))+a.substring(1);

                

            }

            else{

                arr[index]=a;

            }

            index++;

        }

        StringBuilder sb=new StringBuilder();

        for(String b:arr){

            sb.append(b);

            sb.append(" ");

        }

        sb.deleteCharAt(sb.length()-1);

        

        return sb.toString();


TIME COMPLEXITY: O(String Length)[LeetCode Time:12 ms faster than 36.87%]

SPACE COMPLEXITY: O(Total Words In String)[It is a must for us to use a string array to extract all the words present in the string. We have to make one array for that and that is a must.]

AUXILLARY SPACE: O(1)[Memory Used: 43MB less than 5.18%][Only one Array is used in start]

TOTAL TEST CASES:200

APPROACH:

Here first we will extract all the words present in the string using the split() function and store them in the array.

Now we will traverse the Array using forEach loop and then take each element specifically ie String and convert it to lower case using the toLowerCase() function.

(Here in question they have said that weather string size is less than 2 it will be in lower case or if it is more than 2 then also except the first letter whole string will be in lower case.)

Now we will see the size of the string whether it is more than 2 or not.

If it is more than 2 then we will modify the first character to upper case using Character.toUpperCase() function and then using substring function we fix all the characters in lower case.

Later we use the StringBuilder object and append the string with " ".

Question Why do we use the delete function at the end of StringBuilder?

Answer: This is so because earlier we append all the strings present in ArrayList with " " into StringBuilder object so by doing this after last addition also " " will be appended there in StringBuilder.So to remove it we have used deleteCharAt(last index) function.

Later return sb.toString().  



Even though in this case we don't use ArrayList but an array that is a must for splitting string is used. So the memory used by it can't be ignored.





-----------------------------------------------------------------------------------------------------------------------------


"Thanks For Reading.😇"

"Share Further To Increase Knowledge Treasure.😊"

Comments

Popular posts from this blog

Solutions Of Practice Questions Dated 01-06-2022

CODEFORCES SPY DETECTED ROUND 713

Maximum Winning Score Geeks For Geeks