Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
[ad_1]
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
….sEnter: S = “Orange”
Output:
Orange
.vary
..ange
…nge
….ge
…..e
There are 2 methods to print this Triangle sample:
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:
Beneath is this system to print The triangle utilizing for loop:
|
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:
|
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.
[ad_2]