Skip to content
Related Articles
Open in App
Not now

Related Articles

Pattern of Strings

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 22 Sep, 2022
Improve Article
Save Article

Given a string S of length N, find the pattern of the strings as shown below in the examples.

Examples: 

Input: S = “Geek”
Output: Geek, Gee, Ge, G
Explanation: Decrease one character after each line

Input: S = “G*g” 
Output: G*g, G*, G
Explanation: Decrease one character after each line

Using two Nested loops:

Use the following idea to solve the problem:

The idea to solve this problem is based on the fact that we have to print string N time in different lines and print the string by reducing the length of the string from the end each time. 

So, iterate two nested loops one for printing string into different lines for N time and other loop for print the string by reducing the length of the string.

Follow the below steps to solve the problem:

  • Find the length of the given string say N
  • Run a loop on i from 0 till i < N 
    • Run nested loop on j from 0 till j < N – i
      • Print character at the jth index of the string
    • Print the next line character i.e. ‘\n’

Below is the implementation of the above approach.

C++

// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;

// Function to print the pattern
void printPattern(string& s)
{
    // Find the length of the given string
    int n = s.size();

    // Print string for n lines
    for (int i = 0; i < n; i++) {

        // Iterate over the string
        for (int j = 0; j < n - i; j++) {
            cout << s[j];
        }
        cout << '\n';
    }
}

// Driver code
int main()
{
    string s = "GeeK";

    // Function call
    printPattern(s);
    return 0;
}

Java

// Java code to implement the approach
import java.io.*;

class GFG 
{

  // Function to print the pattern
  public static void printPattern(String s)
  {

    // Find the length of the given string
    int n = s.length();

    // Print string for n lines
    for (int i = 0; i < n; i++) {

      // Iterate over the string
      for (int j = 0; j < n - i; j++) {
        System.out.print(s.charAt(j));
      }
      System.out.println();
    }
  }

  // Driver Code
  public static void main(String[] args)
  {
    String s = "GeeK";

    // Function call
    printPattern(s);
  }
}

// This code is contributed by Rohit Pradhan

Python3

# Python3 code to implement the approach
def printPattern(s):

    n = len(s)

    for i in range (0, n):
        for j in range(0, n - i) :
            print(s[j], end = "")
        print("")

# driver code        
s = "GeeK"
printPattern(s)
 
  # This code is contributed by ksam24000

C#

using System;

public class GFG {

  // Function to print the pattern
  public static void printPattern(string s)
  {
    // Find the length of the given string
    int n = s.Length;

    // Print string for n lines
    for (int i = 0; i < n; i++) {

      // Iterate over the string
      for (int j = 0; j < n - i; j++) {
        Console.Write(s[j]);
      }
      Console.WriteLine();
    }
  }

  static public void Main()
  {

    string s = "GeeK";

    // Function call
    printPattern(s);
    // Code
  }
}

// This code is contributed by akashish__

Javascript

<script>
        // JS code to implement the approach

        // Function to print the pattern
        function printPattern(s) 
        {
        
            // Find the length of the given string
            let n = s.length;

            // Print string for n lines
            for (let i = 0; i < n; i++) {

                // Iterate over the string
                for (let j = 0; j < n - i; j++) {
                    document.write(s[j]);
                }
                document.write('</br>');
            }
        }

        // Driver code

        let s = "GeeK";

        // Function call
        printPattern(s);

// This code is contributed by lokeshpotta20.
    </script>
Output

GeeK
Gee
Ge
G

Time Complexity: O(N2)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!