Find Strings formed by replacing prefixes of given String with given characters


In this problem, we will form a triangle from the given string. The triangle will contain the rows equal to the string length - 1, and in each row, we replace the starting characters equal to the row number with '.' Character.

We can use the loop to form each row of the string or string constructor and substr() method.

Problem statement - We have given a string alpha. We need to print the string in the triangle pattern. We need to start the triangle with the alpha string and replace the first character of the previous string with '.' to show it as a triangle.

Sample Example

Input

alpha = 'abc'

Output

abc
.bc
..c

Explanation - In the first row, it prints the string. In the second row, it replaces the first character, and in the third row, it replaces the first two characters.

Input

alpha = 'c'

Output

c

Explanation - It prints the output for the single character.

Input

alpha = “tutorials”

Output

tutorials
.utorials
..torials
...orials
....rials
.....ials
......als
.......ls
........s

Approach 1

In this approach, we will traverse the string, and in each iteration, we will replace the loop index - 1 character with '.' Character, and keep other characters as it is.

Algorithm

  • Step 1 - Initialize the 'str_len' with the string length.

  • Step 2 - Start traversing the string.

  • Step 3 - Initialize the 'temp' string to store the resultant string.

  • Step 4 - Use the nested loop to make 0 to p - 1 iteration to append total p - 1 '.' Characters.

  • Step 5 - Use the for loop to append the characters of the string from the pth index to the len - 1 index to the 'temp' string.

  • Step 6 - Print the temp string.

Example

Following are the Programs to the above approach −

#include <stdio.h>
#include <string.h>

void printTriangle(char* alpha) {
   int str_len = strlen(alpha);
   for (int p = 0; p < str_len; p++) {
      char temp[100]; // Assuming a maximum string length of 100
      memset(temp, 0, sizeof(temp)); // Initialize temp with null characters
      // Append dots to the string
      for (int q = 0; q < p; q++)
         temp[q] = '.';
      // Append character to the string.
      for (int q = p; q < str_len; q++)
         temp[q] = alpha[q];
      // Print the string
      printf("%s\n", temp);
   }
}

int main() {
   char alpha[] = "tutorialspoint";
   printTriangle(alpha);
   return 0;
}

Output

tutorialspoint
.utorialspoint
..torialspoint
...orialspoint
....rialspoint
.....ialspoint
......alspoint
.......lspoint
........spoint
.........point
..........oint
...........int
............nt
.............t
#include <bits/stdc++.h>
using namespace std;

void printTriangle(string alpha) {
   // str_len variable to calculate the length of the string.
   int str_len = alpha.length();
   for (int p = 0; p < str_len; p++) {
      string temp = "";
      // Append dots to the string
      for (int q = 0; q < p; q++)
         temp += ".";
      // Append character to the string.
      for (int q = p; q < str_len; q++)
         temp += alpha[q];
      // Print the string
      cout << temp << "\n";
   }
}
int main(){
   string alpha = "tutorialspoint";
   printTriangle(alpha);
   return 0;
}

Output

tutorialspoint
.utorialspoint
..torialspoint
...orialspoint
....rialspoint
.....ialspoint
......alspoint
.......lspoint
........spoint
.........point
..........oint
...........int
............nt
.............t
public class Main {
   public static void printTriangle(String alpha) {
      int str_len = alpha.length();
      for (int p = 0; p < str_len; p++) {
         StringBuilder temp = new StringBuilder();
         // Append dots to the string
         for (int q = 0; q < p; q++) {
            temp.append(".");
         }
         // Append character to the string.
         for (int q = p; q < str_len; q++) {
            temp.append(alpha.charAt(q));
         }
         // Print the string
         System.out.println(temp.toString());
      }
   }

   public static void main(String[] args) {
      String alpha = "tutorialspoint";
      printTriangle(alpha);
   }
}

Output

tutorialspoint
.utorialspoint
..torialspoint
...orialspoint
....rialspoint
.....ialspoint
......alspoint
.......lspoint
........spoint
.........point
..........oint
...........int
............nt
.............t
def print_triangle(alpha):
   str_len = len(alpha)
   for p in range(str_len):
      temp = ""
      # Append dots to the string
      for q in range(p):
         temp += "."
      # Append character to the string.
      for q in range(p, str_len):
         temp += alpha[q]
      # Print the string
      print(temp)

if __name__ == "__main__":
   alpha = "tutorialspoint"
   print_triangle(alpha)

Output

tutorialspoint
.utorialspoint
..torialspoint
...orialspoint
....rialspoint
.....ialspoint
......alspoint
.......lspoint
........spoint
.........point
..........oint
...........int
............nt
.............t

Time complexity - O(N2), for using nested loops.

Space complexity - O(N) to store the result in the temp string.

Approach 2

In this approach, we will use the String() constructor to create a string with p '.' characters. After that, we will use the substr() method to take the last remaining characters of the string.

Algorithm

  • Step 1 - Use the loop to start traversing the string.

  • Step 2 - use the String() constructor to create a temp string containing the total p number of '.' Characters.

  • Step 3 - Take the substring starting from the pth index, and length equal to the str_len - p, and append to the temp string.

  • Step 4 - Print the temp string.

Example

Following are the programs to solve the above algorithm.

#include <stdio.h>
#include <string.h>

void printTriangle(char* alpha) {
   // str_len variable to calculate the length of the string.
   int str_len = strlen(alpha);
   for (int p = 0; p < str_len; p++) {
      char temp[str_len + 1]; // +1 for null terminator
      memset(temp, '.', p);
      // Append substring starting from index p to len
      strcpy(temp + p, alpha + p);
      temp[str_len] = '\0';
      printf("%s\n", temp);
   }
}

int main() {
   char alpha[] = "tutorials";
   printTriangle(alpha);
   return 0;
}

Output

tutorials
.utorials
..torials
...orials
....rials
.....ials
......als
.......ls
........s
#include <bits/stdc++.h>
using namespace std;

void printTriangle(string alpha) {
   // str_len variable to calculate the length of the string.
   int str_len = alpha.length();   
   for (int p = 0; p < str_len; p++) {
      string temp(p, '.');
      // Append substring starting from index p to len
      temp += alpha.substr(p, str_len - p);
      // Print the string
      cout << temp << "\n";
   }
}
int main() {
   string alpha = "tutorials";
   printTriangle(alpha);
   return 0;
}

Output

tutorials
.utorials
..torials
...orials
....rials
.....ials
......als
.......ls
........s
public class Main {
   public static void printTriangle(String alpha) {
      int str_len = alpha.length();

      for (int p = 0; p < str_len; p++) {
         StringBuilder temp = new StringBuilder();
         for (int i = 0; i < p; i++) {
            temp.append('.'); // Append dots (.) to temp.
         }
         temp.append(alpha.substring(p)); // Append the remaining characters from the input string to temp.
         System.out.println(temp.toString());
      }
   }

   public static void main(String[] args) {
      String alpha = "tutorials";
      printTriangle(alpha); // Call the function to print the triangle.
   }
}

Output

tutorials
.utorials
..torials
...orials
....rials
.....ials
......als
.......ls
........s
def print_triangle(alpha):
   str_len = len(alpha) # Calculate the length of the input string.
   for p in range(str_len):
      temp = '.' * p + alpha[p:] # Create the output string with dots and the remaining characters.
      print(temp) 

if __name__ == "__main__":
   alpha = "tutorials" 
   print_triangle(alpha) # Call the function to print the triangle.

Output

tutorials
.utorials
..torials
...orials
....rials
.....ials
......als
.......ls
........s

Time complexity - O(N2) for traversing the string and getting the substring.

Space complexity - O(N) for storing the temporary string.

We learned to print the triangle pattern using the given string. Programmers can try using the while loop to print the triangle pattern, as we used the for loop in this tutorial. Programmers may use the for loop with the String() constructor.

Updated on: 20-Oct-2023

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements