Given a string str, the task is to make a perfect right-angled triangle by printing the characters of the string.
Input: str = “GEEKSFORGEEKS”
Output :
G
E E
K S F
O R G Enot included E K S because they make our triangle imperfect. i.e.
G
E E
K S F
O R G E
E K SInput: str = “PerfectTriangle”
Output:
P
e r
f e c
t T r i
a n g l e
Approach: To solve the problem follow the above idea:
- Find the point or index up to which a perfect triangle can be made.
- After finding that index implements the nested loop till that index.
- Take a pointer that traverses the string and prints the following character.
Below are the steps for the above approach:
- Initialize a variable say n to store the length of the string.
- Initialize two variables x and y, where x will store the number of rows up to which the outer loop will run and y will store the index up to which the string is used.
- Run a loop till x + y ≤ n, update y = y + x, and increment x i.e the number of rows, to find the index up to which a perfect triangle can be made.
- If x + y – 1 != n, reduce the number of rows by 1.
- Initialize a variable say k = 0 to traverse the string.
- Run a nested loop, outer loop from i = 1 to i ≤ x, and inner loop from j = i to j ≤ i, and now print the characters of the string str[k].
Below is the implementation of the above approach:
C++
// C++ code to make a perfect right angled // triangle from a given string. #include <bits/stdc++.h> using namespace std; // Funtion that prints perfect // right angled triangle void printTriangle(string str) { int n = str.length(); // x is number of rows upto which the // outer loop will run y contains the // index upto which the string is used. int x = 1, y = 1; // Loop to determine the point upto // which string is used while (x + y <= n) { y += x; x++; } // Exclude the unwnated row if (x + y - 1 != n) { x--; } // To travese the given string int k = 0; // Nested loop to make the right // angled triangle for ( int i = 1; i <= x; i++) { for ( int j = 1; j <= i; j++) { cout << str[k] << " " ; k++; } cout << endl; } } // Driver Code int main() { string str = "GEEKSFORGEEKS" ; // Drivers code printTriangle(str); return 0; } |
Java
// Java code to make a perfect right angled // triangle from a given string. import java.util.*; class GFG { // Function that prints perfect // right angled triangle public static void printTriangle(String str) { int n = str.length(); // x is number of rows upto which the // outer loop will run y contains the // index upto which the string is used. int x = 1 , y = 1 ; // Loop to determine the point upto // which string is used while (x + y <= n) { y += x; x++; } // Exclude the unwanted row if (x + y - 1 != n) { x--; } // To traverse the given string int k = 0 ; // Nested loop to make the right // angled triangle for ( int i = 1 ; i <= x; i++) { for ( int j = 1 ; j <= i; j++) { System.out.print(str.charAt(k) + " " ); k++; } System.out.println(); } } // Driver Code public static void main(String[] args) { String str = "GEEKSFORGEEKS" ; // Drivers code printTriangle(str); } } // This code is contributed by prasad264 |
Python3
# Python code to make a perfect right angled # triangle from a given string. # Function that prints perfect # right angled triangle def printTriangle( str ): n = len ( str ) # x is number of rows upto which the # outer loop will run y contains the # index upto which the string is used. x = 1 y = 1 # Loop to determine the point upto # which string is used while x + y < = n: y + = x x + = 1 # Exclude the unwnated row if x + y - 1 ! = n: x - = 1 # To travese the given string k = 0 # Nested loop to make the right # angled triangle for i in range ( 1 , x + 1 ): for j in range ( 1 , i + 1 ): print ( str [k], end = ' ' ) k + = 1 print () # Driver Code if __name__ = = '__main__' : str = "GEEKSFORGEEKS" printTriangle( str ) |
C#
// C# code to make a perfect right angled triangle from a // given string. using System; public class GFG { // Function that prints perfect right angled triangle public static void printTriangle( string str) { int n = str.Length; // x is number of rows upto which the outer loop // will run y contains the index upto which the // string is used. int x = 1, y = 1; // Loop to determine the point upto which string is // used while (x + y <= n) { y += x; x++; } // Exclude the unwanted row if (x + y - 1 != n) { x--; } // To traverse the given string int k = 0; // Nested loop to make the right angled triangle for ( int i = 1; i <= x; i++) { for ( int j = 1; j <= i; j++) { Console.Write(str[k] + " " ); k++; } Console.WriteLine(); } } static public void Main() { // Code string str = "GEEKSFORGEEKS" ; printTriangle(str); } } // This code is contributed by karthik. |
Javascript
// Function that prints perfect // right angled triangle function printTriangle(str) { let n = str.length; // x is number of rows upto which the // outer loop will run y contains the // index upto which the string is used. let x = 1, y = 1; // Loop to determine the point upto // which string is used while (x + y <= n) { y += x; x++; } // Exclude the unwnated row if (x + y - 1 !== n) { x--; } // To travese the given string let k = 0; // Nested loop to make the right // angled triangle for (let i = 1; i <= x; i++) { for (let j = 1; j <= i; j++) { console.log(str[k] + " " ); k++; } console.log( "\n" ); } } // Driver Code let str = "GEEKSFORGEEKS" ; // Drivers code printTriangle(str); |
G E E K S F O R G E
Time Complexity: O (x*x), where x is the point up to which the string is used to make the right-angled triangles.
Auxiliary Space: O(1)
Another Approach:
We can solve the problem using string slicing and repetition.
Steps:
Below are the steps for the above approach:
- First, take the string.
- Then, iterate over the length of the string using a for loop.
- In each iteration of the loop:
- Check if it is possible to print the remaining string in perfect triangle shape.
- If not possible break the loop.
- If possible:
- Slice the input string up to the index j+i+1 form jth index using the string[j:j+i+1] syntax.
- Increment j by i+1.
- Print the repeated substring in a new line using the print() function.
Below is the implementation of the above approach:
C++
#include <iostream> #include <string> // Function that prints perfect // right angled triangle void printTriangle(std::string str) { int j = 0; // Iterate over the length of the string and // print the substring and its repetition for ( int i = 0; i < str.length(); i++) { // To remove imperfect part if (str.length() < (j + i + 1)) { break ; } // Print the triangle std::cout << str.substr(j, i + 1) << std::endl; j += i + 1; } } // Test case int main() { std::string str = "GEEKSFORGEEKS" ; printTriangle(str); return 0; } |
Java
import java.util.*; public class GFG { // Function that prints perfect // right angled triangle public static void printTriangle(String str) { int j = 0 ; // Iterate over the length of the string and // print the substring and its repetition for ( int i = 0 ; i < str.length(); i++) { // To remove imperfect part if (str.length() < (j + i + 1 )) { break ; } // Print the triangle System.out.println(str.substring(j, j + i + 1 )); j += i + 1 ; } } // Test case public static void main(String[] args) { String str = "GEEKSFORGEEKS" ; printTriangle(str); } } |
Python3
# Python code to make a perfect right angled # triangle from a given string. # Function that prints perfect # right angled triangle def printTriangle( str ): j = 0 # Iterate over the length of the string and # print the substring and its repetition for i in range ( len ( str )): # To remove imperfect part if ( len ( str ) < (j + i + 1 )): break # Print the triangle print ( str [j:(j + i + 1 )]) j + = i + 1 # Driver Code if __name__ = = '__main__' : str = "GEEKSFORGEEKS" printTriangle( str ) # This code is contributed by Susobhan Akhuli |
C#
using System; namespace RightAngledTriangle { class Program { // Function that prints perfect // right angled triangle static void PrintTriangle( string str) { int j = 0; // Iterate over the length of the string and // print the substring and its repetition for ( int i = 0; i < str.Length; i++) { // To remove imperfect part if (str.Length < (j + i + 1)) { break ; } // Print the triangle Console.WriteLine(str.Substring(j, i + 1)); j += i + 1; } } // Test case static void Main( string [] args) { string str = "GEEKSFORGEEKS" ; PrintTriangle(str); } } } |
Javascript
// Function that prints perfect // right angled triangle function printTriangle(str) { let j = 0; // Iterate over the length of the string and // print the substring and its repetition for (let i = 0; i < str.length; i++) { // To remove imperfect part if (str.length < (j + i + 1)) { break ; } // Print the triangle console.log(str.slice(j, j + i + 1)); j += i + 1; } } // Test case const str = "GEEKSFORGEEKS" ; printTriangle(str); |
G EE KSF ORGE
Time Complexity: O (x*x), where x is the point up to which the string is used to make the right-angled triangles.
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!