Pattern of Strings
Last Updated :
06 Jul, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void printPattern(string& s)
{
int n = s.size();
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n - i; j++) {
cout << s[j];
}
cout << '\n' ;
}
}
int main()
{
string s = "GeeK" ;
printPattern(s);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static void printPattern(String s)
{
int n = s.length();
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n - i; j++) {
System.out.print(s.charAt(j));
}
System.out.println();
}
}
public static void main(String[] args)
{
String s = "GeeK" ;
printPattern(s);
}
}
|
Python3
def printPattern(s):
n = len (s)
for i in range ( 0 , n):
for j in range ( 0 , n - i) :
print (s[j], end = "")
print ("")
s = "GeeK"
printPattern(s)
|
C#
using System;
public class GFG {
public static void printPattern( string s)
{
int n = s.Length;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n - i; j++) {
Console.Write(s[j]);
}
Console.WriteLine();
}
}
static public void Main()
{
string s = "GeeK" ;
printPattern(s);
}
}
|
Javascript
<script>
function printPattern(s)
{
let n = s.length;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n - i; j++) {
document.write(s[j]);
}
document.write( '</br>' );
}
}
let s = "GeeK" ;
printPattern(s);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Another Approach Using String slicing:
In this method, we use the loop method to loop over the size of the string and the slicing method to print the pattern of the string.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
#include <string>
using namespace std;
void printPattern(string& s)
{
int n = s.size();
for ( int i = n; i > 0; i--) {
cout << s.substr(0, i) << endl;
}
}
int main()
{
string s = "GeeK" ;
printPattern(s);
return 0;
}
|
Java
import java.util.*;
class Main {
public static void printPattern(String s)
{
int n = s.length();
for ( int i = n; i > 0 ; i--) {
System.out.println(s.substring( 0 , i));
}
}
public static void main(String[] args)
{
String s = "GeeK" ;
printPattern(s);
}
}
|
Python
def printPattern(s):
n = len (s)
for i in range ( n, 0 , - 1 ):
print (s[ 0 : i])
s = "GeeK"
printPattern(s)
|
C#
using System;
class GFG {
public static void printPattern(String s)
{
int n = s.Length;
for ( int i = n; i > 0; i--) {
Console.WriteLine(s.Substring(0, i));
}
}
public static void Main()
{
string s = "GeeK" ;
printPattern(s);
}
}
|
Javascript
function printPattern(s)
{
let n = s.length;
for (let i = n; i > 0; i--) {
console.log(s.slice(0,i))
}
}
let s = "GeeK" ;
printPattern(s);
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...