Given a string str, the duty is to print the proper diamond from the weather of the given string.
Enter: str = ” geeksforgeeks”
ok s f
remove ‘eeks’ as a result of it’s not required to make an ideal diamond from the given string
Enter: str = “good”
Method: This may be solved with the next concept:
We want a good sq. quantity equal to or decrease than the size of the string as a result of the sum of all components in diamond house is an ideal sq..
Steps concerned within the implementation of the code:
- Discover the size of the string and examine whether or not it’s a good sq. of a quantity or not.
- If not an ideal sq. then finds the simply decrease good sq. and retailer it in a variable say len and n containing the sq. root of that quantity.
- Take a variable ch that traverses the given string and print the weather
- We require two nested loops.
- First to make an higher triangle of a diamond and one other is to print the decrease triangle of a diamond.
- In each the nested loop there are additional loops one is for printing the entrance areas and one other is to print the character.
Beneath is the implementation of the above strategy:
Time complexity: O(n*n*n)
Auxiliary House: O(1)