How to use two Nested loops In Data Structures and Algorithms
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’
- Run nested loop on j from 0 till j < N – i
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> |
GeeK Gee Ge G
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++
// C++ program to implement // the above approach #include <bits/stdc++.h> #include <string> using namespace std; void printPattern(string& s) { // Find the length of the given string int n = s.size(); // Print string for n lines for ( int i = n; i > 0; i--) { // print pattern of string cout << s.substr(0, i) << endl; } } // Driver code int main() { string s = "GeeK" ; // Function call printPattern(s); return 0; } |
Java
// Java program to implement the above approach import java.util.*; class Main { 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 = n; i > 0 ; i--) { // print pattern of string System.out.println(s.substring( 0 , i)); } } // Driver code public static void main(String[] args) { String s = "GeeK" ; // Function call printPattern(s); } } |
Python
# Python3 code to implement the approach def printPattern(s): n = len (s) for i in range ( n, 0 , - 1 ): print (s[ 0 : i]) # driver code s = "GeeK" printPattern(s) |
C#
// C# program to implement the above approach using System; class GFG { 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 = n; i > 0; i--) { // print pattern of string Console.WriteLine(s.Substring(0, i)); } } // Driver code public static void Main() { string s = "GeeK" ; // Function call printPattern(s); } } // This code is contributed by Vaibhav Nandan |
Javascript
// 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 = n; i > 0; i--) { // print patter of string console.log(s.slice(0,i)) } } // Driver code let s = "GeeK" ; // Function call printPattern(s); |
GeeK Gee Ge G
Time Complexity: O(N2)
Auxiliary Space: O(1)
Pattern of Strings
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 lineInput: S = “G*g”
Output: G*g, G*, G
Explanation: Decrease one character after each line