Remove Leading Zeroes from an IP Address Geeks For Geeks

 Problem Link:


https://practice.geeksforgeeks.org/problems/remove-leading-zeros-from-an-ip-address3530/1#


It is also the problem of the day for 11-02-2020



Solution:


Today we will first discuss the approach and then we will move on to the solution.


1. We are given a string consisting of "." so to access each word we need to separate the words present in the string. For splitting the words from the string we will use the split function and its syntax will be split("\\.")

2. Now for splitting we will use one string array and store all strings in it.

3. After splitting we need to look at every word and remove all the leading zeroes from them.

4. For removing leading zeroes we have 2 different ways:

    a)traverse string till while(str.charAt(i)!=0) and simultaneously remove characters(You will require         stringbuilder for character deletions)

    b)Use Integer.valueOf or Integer.parseInt() to directly get the values without leading zeroes as all leading zeroes get removed when the string is converted to an integer.

5. It may be possible that numbers present in the string may be very large so a Number format exception may occur for example 12243231231331. Similarly, the Number format exception may also occur if we use a long data type for example 1544432412412415141414151.

6. Now we need to think of a solution in which large values are handled itself and for that BigInteger is an option.


So this is all the things you need to keep in mind and then we write the code:


Solution First:

Using StringBuilder :


String arr[];

        arr=s.split("\\.");

        

        String p=(new java.math.BigInteger(arr[0]))+".";

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

            p=p+new java.math.BigInteger(arr[i])+".";

        }

        StringBuilder sb=new StringBuilder();

        sb.append(p);

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

        return sb.toString();


Without Using StringBuilder:


String arr[];

        arr=s.split("\\.");

        

        String p=(new java.math.BigInteger(arr[0])).toString()+"";

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

            p=p+"."+new java.math.BigInteger(arr[i]);

        }

        

       return p; 


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

[Here we can not import math.BigInteger class that's why we are writing new math.BigInteger() object directly.

Moreover, we are first storing the value of the first word (after removing leading zeroes) in string p so that we will use it later and append all the other words in that existing string p.

Usage Of StringBuilder in the last case will take some more time as compared to without using it. However, it will not be much as StringBuilder is an inbuilt data type in java and its function will take constant time ex deleteCharAt(index)].




Time Complexity: O(String Length)[GFG Time: 0.4/1.5][Traversal of the string will be there.]

Space Complexity: O(Total Words In String)[We need to use an array for fetching words after splitting them from a given string using the split function.]

Auxiliary Space: O(1)[Either StringBuilder is used or a string directly is used.]

Total Test Cases:150




"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