Open In App
Related Articles

Create a prefect diamond from the elements of the given String

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Given a string str, the task is to print the perfect diamond from the elements of the given string.

Examples:

Input: str = ” geeksforgeeks”
Output:  
   g 
 e  e 
k  s  f 
 o  r 
  g 
eliminate ‘eeks’ because it is not required to make a perfect diamond from the given string

Input: str =  “good”
Output:
 g 
o o 
 d 

Approach: This can be solved with the following idea:

We need a perfect square number equal to or lower than the length of the string because the sum of all elements in diamond space is a perfect square.

Steps involved in the implementation of the code:

  • Find the length of the string and check whether it is a perfect square of a number or not.
  • If not a perfect square then finds the just lower perfect square and store it in a variable say len and n containing the square root of that number.
  • Take a variable ch that traverses the given string and print the elements
  • We require two nested loops.
  • First to make an upper triangle of a diamond and another is to print the lower triangle of a diamond.
  • In both the nested loop there are further loops one is for printing the front spaces and another is to print the character.

Below is the implementation of the above approach:

C++

// C++ code to print the perfect diamond
// from elements of the given string
#include <bits/stdc++.h>
using namespace std;
 
// Function to print diamond
void perfectDiamond(string str)
{
 
    // Find length
    int num = str.length();
 
    // Find square root to determine the
    // required length of string to
    // make perfect diamond
    int n = sqrt(num);
 
    // Find required length of string
    // to make perfect diamond
    int len = n * n;
 
    // To traverse and print
    // the given string
    int ch = 0;
 
    // To print the upper
    // triangle of diamond
    for (int i = 1; i <= n; i++) {
 
        // Loop that print spaces
        for (int k = n - 1; k >= i; k--) {
            cout << " ";
        }
 
        // Loop to print particular number
        // of character from string
        for (int j = 1; j <= i; j++) {
            cout << str[ch] << " ";
            ch++;
        }
        cout << endl;
    }
 
    // To print lower triangle of diamond
    for (int i = 1; i < n; i++) {
 
        // Loop to print spaces
        for (int k = 1; k <= i; k++) {
            cout << " ";
        }
 
        // Loop to print particular number
        // of character from string
        for (int j = n - 1; j >= i; j--) {
            cout << str[ch] << " ";
            ch++;
        }
        cout << endl;
    }
}
 
// Driver code
int main()
{
 
    string str = "geeksforgeeks";
 
    // Function call
    perfectDiamond(str);
 
    return 0;
}

                    

Java

// Java code to print the perfect diamond
// from elements of the given string
import java.lang.Math;
 
public class Main {
 
    // Function to print diamond
    public static void perfectDiamond(String str) {
 
        // Find length
        int num = str.length();
 
        // Find square root to determine the
        // required length of string to
        // make perfect diamond
        int n = (int) Math.sqrt(num);
 
        // Find required length of string
        // to make perfect diamond
        int len = n * n;
 
        // To traverse and print
        // the given string
        int ch = 0;
 
        // To print the upper
        // triangle of diamond
        for (int i = 1; i <= n; i++) {
 
            // Loop that prints spaces
            for (int k = n - 1; k >= i; k--) {
                System.out.print(" ");
            }
 
            // Loop to print particular number
            // of character from string
            for (int j = 1; j <= i; j++) {
                System.out.print(str.charAt(ch) + " ");
                ch++;
            }
            System.out.println();
        }
 
        // To print lower triangle of diamond
        for (int i = 1; i < n; i++) {
 
            // Loop to print spaces
            for (int k = 1; k <= i; k++) {
                System.out.print(" ");
            }
 
            // Loop to print particular number
            // of character from string
            for (int j = n - 1; j >= i; j--) {
                System.out.print(str.charAt(ch) + " ");
                ch++;
            }
            System.out.println();
        }
    }
 
    // Driver code
    public static void main(String[] args) {
 
        String str = "geeksforgeeks";
 
        // Function call
        perfectDiamond(str);
 
    }
}

                    

Python3

import math
 
# Function to print diamond
def perfectDiamond(str):
 
    # Find length
    num = len(str)
 
    # Find square root to determine the
    # required length of string to
    # make perfect diamond
    n = int(math.sqrt(num))
 
    # Find required length of string
    # to make perfect diamond
    len_str = n * n
 
    # To traverse and print
    # the given string
    ch = 0
 
    # To print the upper
    # triangle of diamond
    for i in range(1, n+1):
 
        # Loop that print spaces
        for k in range(n-1, i-1, -1):
            print(" ", end="")
 
        # Loop to print particular number
        # of character from string
        for j in range(1, i+1):
            print(str[ch], end=" ")
            ch += 1
        print()
 
    # To print lower triangle of diamond
    for i in range(1, n):
 
        # Loop to print spaces
        for k in range(0, i):
            print(" ", end="")
 
        # Loop to print particular number
        # of character from string
        for j in range(n-1, i-1, -1):
            print(str[ch], end=" ")
            ch += 1
        print()
 
# Driver code
if __name__ == '__main__':
 
    str = "geeksforgeeks"
 
    # Function call
    perfectDiamond(str)

                    

C#

using System;
 
class Program
{
    // Function to print diamond
    static void perfectDiamond(string str)
    {
        // Find length
        int num = str.Length;
 
        // Find square root to determine the
        // required length of string to
        // make perfect diamond
        int n = (int)Math.Sqrt(num);
 
        // Find required length of string
        // to make perfect diamond
        int len = n * n;
 
        // To traverse and print
        // the given string
        int ch = 0;
 
        // To print the upper
        // triangle of diamond
        for (int i = 1; i <= n; i++)
        {
            // Loop that print spaces
            for (int k = n - 1; k >= i; k--)
            {
                Console.Write(" ");
            }
 
            // Loop to print particular number
            // of character from string
            for (int j = 1; j <= i; j++)
            {
                Console.Write(str[ch] + " ");
                ch++;
            }
            Console.WriteLine();
        }
 
        // To print lower triangle of diamond
        for (int i = 1; i < n; i++)
        {
            // Loop to print spaces
            for (int k = 1; k <= i; k++)
            {
                Console.Write(" ");
            }
 
            // Loop to print particular number
            // of character from string
            for (int j = n - 1; j >= i; j--)
            {
                Console.Write(str[ch] + " ");
                ch++;
            }
            Console.WriteLine();
        }
    }
 
    static void Main()
    {
        string str = "geeksforgeeks";
 
        // Function call
        perfectDiamond(str);
    }
}

                    

Javascript

// Function to print diamond
function perfectDiamond(str) {
 
    // Find length
    var num = str.length;
     
    // Find square root to determine the
    // required length of string to
    // make perfect diamond
    var n = Math.sqrt(num);
     
    // Find required length of string
    // to make perfect diamond
    var len_str = n * n;
     
    // Find required length of string
    // to make perfect diamond
    var ch = 0;
     
     
    // To print the upper
    // triangle of diamond
    for (var i = 1; i <= n; i++) {
     
        // Loop that print spaces
        for (var k = n - 1; k >= i - 1; k--) {
            process.stdout.write(" ");
        }
         
        // Loop to print particular number
        // of character from string
        for (var j = 1; j <= i; j++) {
            process.stdout.write(str[ch] + " ");
            ch += 1;
        }
        console.log();
    }
     
    // To print lower triangle of diamond
    for (var i = 1; i < n; i++) {
     
        // Loop to print spaces
        for (var k = 0; k < i; k++) {
            process.stdout.write(" ");
        }
         
         
        // Loop to print particular number
        // of character from string
        for (var j = n - 1; j >= i - 1; j--) {
            process.stdout.write(str[ch] + " ");
            ch += 1;
        }
        console.log();
    }
}
 
// Test case
var str = "geeksforgeeks";
perfectDiamond(str);

                    

Output
  g 
 e e 
k s f 
 o r 
  g 

Time complexity: O(n*n*n)
Auxiliary Space: O(1)



Last Updated : 26 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads