Given a string str, the duty is to print an ideal pyramid by printing the characters of the given string.
Examples:
Enter: str = “GEEKSFORGEEKS”
Output:
G
E E Ok
S F O R Gnot together with E Ok S as a result of they make our pyramid imperfect. i.e.
G
E E Ok
S F O R G
E Ok SEnter: str = “PyramidOfStrings”
Output:
P
y r a
m i d O f
S t r i n g s
Method: To resolve the issue observe the above thought:
- Discover the rows as much as which an ideal pyramid may be made.
- After discovering the rows and variety of components used within the string, implements the nested loop until that row.
- Take a pointer that traverses the string and prints the next character.
Beneath are the steps for the above strategy:
- Initialize a variable say n to retailer the size of the string.
- Initialize two variables x and ele, the place x will retailer the variety of components in a selected row and ele will retailer the variety of complete components used from string until that row as much as that row.
- Initialize variable rows as much as which the outer loop will run.
- There are 2 inside loops one for printing the beginning areas of each row and one other row for printing the character.
- Take a pointer ch that traverses the string and prints the next character.
- In each nth row there are 2*row-1 components So print the character in a row till okay != 2*row-1.
Beneath is the implementation of the above strategy:
C++
|
P y r a m i d O f S t r i n g s
Time Complexity: O (rows*x), the place rows are the entire variety of rows and x is the entire variety of components in a selected row.
Auxiliary Area: O(1)