Find Strings formed by replacing prefixes of given String with given characters
Last Updated :
21 Nov, 2022
Given a String ( S ) of length N and with that String, we need to print a triangle out of it. The triangle should start with the given string and keeps shrinking downwards by removing one character from the beginning of the string. The spaces on the left side of the triangle should be replaced with dot characters ( '.' ).
Examples:
Input: S = "Geeks"
Output:
Geeks
.eeks
..eks
...ks
....s
Input: S = "Orange"
Output:
Orange
.range
..ange
...nge
....ge
.....e
There are 2 ways to print this Triangle pattern:
- Using For Loop.
- Using While Loop.
Let's start discussing each of these methods in detail.
Approach:
The approach is to use three loops:
- One is to control the number of rows.
- The second is to control the dots before the String.
- The third is to Print the remaining String.
By using the concepts of the nested loop wecan easily print the pattern.
Follow the steps to solve the problem:
- Take the Input of the String S.
- Use three loops one is the outer loop to change the line and two inner loops one to print the dots before the String and the other to print the remaining String.
- The Outer loop ( i ) runs from 0 to length -1 times.
- The First Inner loop runs from 0 to i times to print the dot character.
- The Second Inner loop runs from i to length - 1 time to print the remaining String.
- After these two loops print the string that is formed.
Below is the program to print The triangle using for loop:
C++
// Program to Print a,
// Triangle Pattern using a string S.
#include <bits/stdc++.h>
using namespace std;
void fun(string S)
{
// str variable to make a new string.
string str = "";
// len variable to calculate the length of the string.
int len = S.length();
// Outer loop to control the rows.
for (int i = 0; i < len; i++) {
str = "";
// First Inner loop to print the dot character.
for (int j = 0; j < i; j++)
str += ".";
// Second Inner loop to print the remaining string,
for (int j = i; j < len; j++)
str += S[j];
// Printing the row and going to the next line.
cout << str << "\n";
}
}
// Drivers code
int main()
{
string S = "Geeks";
// Function call
fun(S);
return 0;
}
Java
/*package whatever //do not write package name here */
// Program to Print a,
// Triangle Pattern using a string S.
import java.io.*;
class GFG {
static void fun(String S)
{
// str variable to make a new string.
String str = "";
// len variable to calculate the length of the string.
int len = S.length();
// Outer loop to control the rows.
for (int i = 0; i < len; i++) {
str = "";
// First Inner loop to print the dot character.
for (int j = 0; j < i; j++)
str += ".";
// Second Inner loop to print the remaining string,
for (int j = i; j < len; j++)
str += S.charAt(j);
// Printing the row and going to the next line.
System.out.println(str);
}
}
public static void main (String[] args) {
String S="Geeks";
// function call
fun(S);
}
}
// This code is contributed by nmkiniqw7b.
Python3
# Python code
# Triangle Pattern using a string S.
def fun(S):
# str variable to make a new string.
str = ""
# len variable to calculate the length of the string.
l =len(S)
# Outer loop to control the rows.
for i in range(0,l):
str = ""
# First Inner loop to print the dot character.
for j in range(0,i):
str += "."
# Second Inner loop to print the remaining string,
for j in range(i,l):
str += S[j]
# Printing the row and going to the next line.
print(str)
# Drivers code
S = "Geeks"
# Function call
fun(S)
# This code is contributed by ksam24000
C#
// C# implementation
using System;
public class GFG {
public static void fun(string S)
{
// str variable to make a new string.
string str = "";
// len variable to calculate the length of the
// string.
int len = S.Length;
// Outer loop to control the rows.
for (int i = 0; i < len; i++) {
str = "";
// First Inner loop to print the dot character.
for (int j = 0; j < i; j++)
str += ".";
// Second Inner loop to print the remaining
// string,
for (int j = i; j < len; j++)
str += S[j];
// Printing the row and going to the next line.
Console.WriteLine(str);
}
}
static public void Main()
{
string S = "Geeks";
// Function call
fun(S);
}
}
// this code is contributed by ksam24000
JavaScript
// JavaScript implementation
// Triangle Pattern using a string S.
function fun(S)
{
// str variable to make a new string.
let str = "";
// len variable to calculate the length of the string.
let len = S.length;
// Outer loop to control the rows.
for (let i = 0; i < len; i++) {
str = "";
// First Inner loop to print the dot character.
for (let j = 0; j < i; j++)
str += ".";
// Second Inner loop to print the remaining string,
for (let j = i; j < len; j++)
str += S[j];
// Printing the row and going to the next line.
console.log (str);
}
}
// Drivers code
S = "Geeks";
// Function call
fun(S);
// this code is contributed by ksam24000
OutputGeeks
.eeks
..eks
...ks
....s
Time Complexity: O(N2), as the nested loop, is used.
Auxiliary Space: O(N), as we are creating a new string and reusing it.
Below is the program to print the triangle using the while loop:
C++
// Program to Print a,
// Triangle Pattern using a string S.
#include <bits/stdc++.h>
using namespace std;
void fun(string S)
{
// str variable to make a new string.
string str = "";
// len variable to calculate the
// length of the string.
int len = S.length();
// Outer loop to control the rows.
int i = 0;
while (i < len) {
str = "";
// First Inner loop to print
// the dot character.
int j = 0;
while (j < i) {
str += ".";
j++;
}
// Second Inner loop to print
// the remaining string,
int k = i;
while (k < len) {
str += S[k];
k++;
}
// Printing the row and going
// to the next line.
cout << str << "\n";
// Incrementing the loop
// control variable.
i++;
}
}
// Drivers code
int main()
{
string S = "Geeks";
// Function call
fun(S);
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void fun(String S)
{
// str variable to make a new string.
String str = "";
// len variable to calculate the
// length of the string.
int len = S.length();
// Outer loop to control the rows.
int i = 0;
while (i < len) {
str = "";
// First Inner loop to print
// the dot character.
int j = 0;
while (j < i) {
str += ".";
j++;
}
// Second Inner loop to print
// the remaining string,
int k = i;
while (k < len) {
str += S.charAt(k);
k++;
}
// Printing the row and going
// to the next line.
System.out.println(str);
// Incrementing the loop
// control variable.
i++;
}
}
public static void main (String[] args)
{
// Code
String S = "Geeks";
// Function call
fun(S);
}
}
// This code is contributed by aadityaburujwale.
Python3
# Program to Print a,
# Triangle Pattern using a string S.
def fun(S):
# str variable to make a new string.
str = ""
# len variable to calculate the
# length of the string.
length = len(S);
# Outer loop to control the rows.
i = 0
while (i < length):
str = ""
# First Inner loop to print
# the dot character.
j = 0
while (j < i):
str += "."
j=j+1
# Second Inner loop to print
# the remaining string,
k = i
while (k < length):
str += S[k]
k += 1
# Printing the row and going
# to the next line.
print(str)
# Incrementing the loop
# control variable.
i+=1
# Drivers code
S = "Geeks"
# Function call
fun(S)
# This code is contributed by akashish__
C#
using System;
public class GFG {
public static void fun(string S)
{
// str variable to make a new string.
string str = "";
// len variable to calculate the
// length of the string.
int len = S.Length;
// Outer loop to control the rows.
int i = 0;
while (i < len) {
str = "";
// First Inner loop to print
// the dot character.
int j = 0;
while (j < i) {
str += ".";
j++;
}
// Second Inner loop to print
// the remaining string,
int k = i;
while (k < len) {
str += S[k];
k++;
}
// Printing the row and going
// to the next line.
Console.WriteLine(str);
// Incrementing the loop
// control variable.
i++;
}
}
// Drivers code
static public void Main()
{
// Code
string S = "Geeks";
// Function call
fun(S);
}
}
// contributed by akashish__
JavaScript
<script>
// Program to Print a,
// Triangle Pattern using a string S.
function fun(S) {
// str variable to make a new string.
let str = "";
// len variable to calculate the
// length of the string.
let len = S.length;
// Outer loop to control the rows.
let i = 0;
while (i < len) {
str = "";
// First Inner loop to print
// the dot character.
let j = 0;
while (j < i) {
str += ".";
j++;
}
// Second Inner loop to print
// the remaining string,
let k = i;
while (k < len) {
str += S[k];
k++;
}
// Printing the row and going
// to the next line.
console.log(str);
// Incrementing the loop
// control variable.
i++;
}
}
// Drivers code
let S = "Geeks";
// Function call
fun(S);
// This code is contributed by akashish__
</script>
OutputGeeks
.eeks
..eks
...ks
....s
Time Complexity: O(N2), as the nested loop, is used.
Auxiliary Space: O(1), as we are creating a new string and reusing it.
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms
DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Quick Sort
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Breadth First Search or BFS for a Graph
Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Bubble Sort Algorithm
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Binary Search Algorithm - Iterative and Recursive Implementation
Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Data Structures Tutorial
Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all
Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read