Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Friday, July 21, 2017

print all possible string geeks solution


Problem: input : abc

process like :
               abc
                ab c
                a bc
                a b c


Code:


/*You have to complete this function*/
class GfG
{
    void print(String str,char temp[],int i,int j,int n)
    {
        if(i==n)
        {
            temp[j]='$';
            for(int p=0;p<=j;p++)
            System.out.print(temp[p]);
            return;
        }
        temp[j]=str.charAt(i);
        print(str,temp,i+1,j+1,n);
        temp[j]=' ';
         temp[j+1]=str.charAt(i);
        print(str,temp,i+1,j+2,n);
    }
    void printSpace(String str)
    {
        // Your code here
        char temp[]=new char[str.length()*2];
        temp[0]=str.charAt(0);
        print(str,temp,1,1,str.length());
    }
}

0 Comments:

Post a Comment

Stats