Make a perfect right angled triangle by printing characters of given String
Last Updated :
13 Sep, 2023
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 E
not included E K S because they make our triangle imperfect. i.e.
G
E E
K S F
O R G E
E K S
Input: 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++
#include <bits/stdc++.h>
using namespace std;
void printTriangle(string str)
{
int n = str.length();
int x = 1, y = 1;
while (x + y <= n) {
y += x;
x++;
}
if (x + y - 1 != n) {
x--;
}
int k = 0;
for ( int i = 1; i <= x; i++) {
for ( int j = 1; j <= i; j++) {
cout << str[k] << " " ;
k++;
}
cout << endl;
}
}
int main()
{
string str = "GEEKSFORGEEKS" ;
printTriangle(str);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void printTriangle(String str)
{
int n = str.length();
int x = 1 , y = 1 ;
while (x + y <= n) {
y += x;
x++;
}
if (x + y - 1 != n) {
x--;
}
int k = 0 ;
for ( int i = 1 ; i <= x; i++) {
for ( int j = 1 ; j <= i; j++) {
System.out.print(str.charAt(k) + " " );
k++;
}
System.out.println();
}
}
public static void main(String[] args)
{
String str = "GEEKSFORGEEKS" ;
printTriangle(str);
}
}
|
Python3
def printTriangle( str ):
n = len ( str )
x = 1
y = 1
while x + y < = n:
y + = x
x + = 1
if x + y - 1 ! = n:
x - = 1
k = 0
for i in range ( 1 , x + 1 ):
for j in range ( 1 , i + 1 ):
print ( str [k], end = ' ' )
k + = 1
print ()
if __name__ = = '__main__' :
str = "GEEKSFORGEEKS"
printTriangle( str )
|
C#
using System;
public class GFG {
public static void printTriangle( string str)
{
int n = str.Length;
int x = 1, y = 1;
while (x + y <= n) {
y += x;
x++;
}
if (x + y - 1 != n) {
x--;
}
int k = 0;
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()
{
string str = "GEEKSFORGEEKS" ;
printTriangle(str);
}
}
|
Javascript
function printTriangle(str) {
let n = str.length;
let x = 1, y = 1;
while (x + y <= n) {
y += x;
x++;
}
if (x + y - 1 !== n) {
x--;
}
let k = 0;
for (let i = 1; i <= x; i++) {
for (let j = 1; j <= i; j++) {
console.log(str[k] + " " );
k++;
}
console.log( "\n" );
}
}
let str = "GEEKSFORGEEKS" ;
printTriangle(str);
|
Output
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>
void printTriangle(std::string str) {
int j = 0;
for ( int i = 0; i < str.length(); i++) {
if (str.length() < (j + i + 1)) {
break ;
}
std::cout << str.substr(j, i + 1) << std::endl;
j += i + 1;
}
}
int main() {
std::string str = "GEEKSFORGEEKS" ;
printTriangle(str);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void printTriangle(String str) {
int j = 0 ;
for ( int i = 0 ; i < str.length(); i++) {
if (str.length() < (j + i + 1 )) {
break ;
}
System.out.println(str.substring(j, j + i + 1 ));
j += i + 1 ;
}
}
public static void main(String[] args) {
String str = "GEEKSFORGEEKS" ;
printTriangle(str);
}
}
|
Python3
def printTriangle( str ):
j = 0
for i in range ( len ( str )):
if ( len ( str ) < (j + i + 1 )):
break
print ( str [j:(j + i + 1 )])
j + = i + 1
if __name__ = = '__main__' :
str = "GEEKSFORGEEKS"
printTriangle( str )
|
C#
using System;
namespace RightAngledTriangle {
class Program {
static void PrintTriangle( string str)
{
int j = 0;
for ( int i = 0; i < str.Length; i++) {
if (str.Length < (j + i + 1)) {
break ;
}
Console.WriteLine(str.Substring(j, i + 1));
j += i + 1;
}
}
static void Main( string [] args)
{
string str = "GEEKSFORGEEKS" ;
PrintTriangle(str);
}
}
}
|
Javascript
function printTriangle(str) {
let j = 0;
for (let i = 0; i < str.length; i++) {
if (str.length < (j + i + 1)) {
break ;
}
console.log(str.slice(j, j + i + 1));
j += i + 1;
}
}
const str = "GEEKSFORGEEKS" ;
printTriangle(str);
|
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...