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
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
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
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
Get K-th letter of the decoded string formed by repeating substrings Given a string S containing letter and digit and an integer K where, 2\leq S.length() \leq 100 and 1\leq K \leq 10^{9} . The task is to return the K-th letter of the new string S'.The new string S' is formed from old string S by following steps: 1. If the character read is a letter, that letter is a
6 min read
Make string S equal to T after replacing some prefix characters in S Given two strings S and T of equal length consisting of lowercase characters. In one operation, it is allowed to erase the 0th character of the string S and move it to any place in the string S, the task is to find the minimum number of operations required to convert S equal to T. Examples: Input: S
7 min read
Minimize length of a string by removing suffixes and prefixes of same characters Given a string S of length N consisting only of characters 'a', 'b', and 'c', the task is to minimize the length of the given string by performing the following operations only once: Divide the string into two non-empty substrings and then, append the left substring to the end of the right substring
6 min read
Remove minimum characters from string to split it into three substrings under given constraints Given a string str of lowercase alphabets, the task is to remove minimum characters from the given string so that string can be break into 3 substrings str1, str2, and str3 such that each substring can be empty or can contains only characters 'a', 'b', and 'c' respectively.Example: Input: str = "aaa
8 min read
Python - Replacing Nth occurrence of multiple characters in a String with the given character Replacing the Nth occurrence of multiple characters in a string with a given character involves identifying and counting specific character occurrences.Using a Loop and find()Using a loop and find() method allows us to search for the first occurrence of a substring within each list element. This app
2 min read