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
Encode given string by replacing substrings with prefix same as itself with *
Given string str of size N containing only lowercase English letters. The task is to encrypt the string such that the substrings having same prefix as itself are replaced by a *. Generate the encrypted string. Note: If the string can be encrypted in multiple ways, find the smallest encrypted string.
9 min read
Check if String T can be made Substring of S by replacing given characters
Given two strings S and T and a 2D array replace[][], where replace[i] = {oldChar, newChar} represents that the character oldChar of T is replaced with newChar. The task is to find if it is possible to make string T a substring of S by replacing characters according to the replace array. Note: Each
9 min read
Find the Suffix Array of given String with no repeating character
Given a string str of size N, the task is to find the suffix array of the given string. Note: A suffix array is a sorted array of all suffixes of a given string. Examples: Input: str = "prince"Output: 4 5 2 3 0 1Explanation: The suffixes are0 prince 4 ce1 rince Sort the suffixes 5 e 2 ince ---------
6 min read
Decrypt message from given code by replacing all * with prefix values of encoded string
Given a string str of length of N that is in the encoded form with alphabets and * . The task is to find the string from which it was generated. The required string can be generated from the encoded string by replacing all the * with the prefix values of the encoded string. Examples: Input: str = ab
4 min read
Replace the given Strings starting from given indices
Given a string S on which you need to perform Q replace operations.Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then \replace that occurrence of x with y. Note: All these ope
6 min read
Lexicographically largest string formed in minimum moves by replacing characters of given String
Given a string S consisting of N lowercase English characters, the task is to print the lexicographically, the largest string obtained using only the minimum number of moves needed to modify the string S to a string containing the first min(N, 26) lower case English alphabet, by replacing any charac
10 min read
Convert given Strings into T by replacing characters in between strings any number of times
Given an array, arr[] of N strings and a string T of size M, the task is to check if it is possible to make all the strings in the array arr[] same as the string T by removing any character from one string, say arr[i] and inserting it at any position to another string arr[j] any number of times. Exa
9 min read
Find Array formed by reversing Prefix each time given character is found
Given an array arr[] of length N consisting of uppercase English letters only and a letter ch. the task is to find the final array that will form by reversing the prefix each time the letter ch is found in the array. Examples: Input: arr[] = {'A', 'B', 'X', 'C', 'D', 'X', 'F'}, ch= 'X'Output: D C X
6 min read
Minimum count of prefixes and suffixes of a string required to form given string
Given two strings str1 and str2, the task is to find the minimum number of prefixes and suffixes of str2 required to form the string str1. If the task is not possible, return "-1".Example: Input: str1 = "HELLOWORLD", str2 = "OWORLDHELL"Output: 2Explanation: The above string can be formed as "HELL" +
10 min read
Find smallest string with whose characters all given Strings can be generated
Given an array of strings arr[]. The task is to generate the string which contains all the characters of all the strings present in array and smallest in size. There can be many such possible strings and any one is acceptable. Examples: Input: arr[] = {"your", "you", "or", "yo"}Output: ruyoExplanati
5 min read