Discover Strings fashioned by changing prefixes of given String with given characters


View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Given a String ( S ) of size N and with that String, we have to print a triangle out of it. The triangle ought to begin with the given string and retains shrinking downwards by eradicating one character from the start of the string. The areas on the left facet of the triangle needs to be changed with dot characters ( ‘.’ ).

Examples:

Enter: S = “Geeks”
Output
Geeks
.eeks
..eks
…ks
….s

Enter: S = “Orange”
Output: 
Orange
.vary
..ange
…nge
….ge
…..e

There are 2 methods to print this Triangle sample:

  • Utilizing For Loop.
  • Utilizing Whereas Loop.

Let’s begin discussing every of those strategies intimately.

Method:

The strategy is to make use of three loops:

  • One is to manage the variety of rows.
  • The second is to manage the dots earlier than the String.
  • The third is to Print the remaining String.

By utilizing the ideas of the nested loop wecan simply print the sample.

Observe the steps to resolve the issue:

  • Take the Enter of the String S.
  • Use three loops one is the outer loop to vary the road and two inside loops one to print the dots earlier than the String and the opposite to print the remaining String.
  • The Outer loop ( i ) runs from 0 to size -1 occasions.
    • The First Inside loop runs from 0 to i occasions to print the dot character.
    •  The Second Inside loop runs from i to size – 1 time to print the remaining String.
    • After these two loops print the string that’s fashioned.

Beneath is this system to print The triangle utilizing for loop:

C++

#embody <bits/stdc++.h>

utilizing namespace std;

void enjoyable(string S)

{

    

    string str = "";

  

    

    int len = S.size();

  

    

    for (int i = 0; i < len; i++) {

        str = "";

  

        

        for (int j = 0; j < i; j++)

            str += ".";

  

        

        for (int j = i; j < len; j++)

            str += S[j];

  

        

        cout << str << "n";

    }

}

  

int predominant()

{

    string S = "Geeks";

  

    

    enjoyable(S);

  

    return 0;

}

Output

Geeks
.eeks
..eks
...ks
....s

Time Complexity: O(N2), because the nested loop, is used.
Auxiliary House: O(N), as we’re creating a brand new string and reusing it.

Beneath is this system to print the triangle utilizing the whereas loop:

C++

#embody <bits/stdc++.h>

utilizing namespace std;

void enjoyable(string S)

{

  

    

    string str = "";

  

    

    

    int len = S.size();

  

    

    int i = 0;

    whereas (i < len) {

        str = "";

  

        

        

        int j = 0;

        whereas (j < i) {

            str += ".";

            j++;

        }

  

        

        

        int okay = i;

        whereas (okay < len) {

            str += S[k];

            okay++;

        }

        

        

        cout << str << "n";

  

        

        

        i++;

    }

}

  

int predominant()

{

    string S = "Geeks";

  

    

    enjoyable(S);

  

    return 0;

}

Output

Geeks
.eeks
..eks
...ks
....s

Time Complexity: O(N2), because the nested loop, is used.
Auxiliary House: O(1), as we’re creating a brand new string and reusing it.

Leave a Reply