Sunday, March 5, 2023
HomeSoftware DevelopmentPrint all Substrings of size n doable from the given String

Print all Substrings of size n doable from the given String


Given a string str and an integer N, the duty is to print all doable sub-strings of size N.

Examples:

Enter: str = “geeksforgeeks”, N = 3
Output: gee eek eks ksf sfo for org rge gee eek eks
Explanations: All doable sub-strings of size 3 are “gee”, “eek”, “eks”, “ksf”, “sfo”, “for”, “org”, “rge”, “gee”, “eek” and “eks”.

Enter: str = “GFG”, N = 2 
Output: GF FG
Explanations: All doable sub-strings of size 2 are “GF”, “FG”

Technique 1: Utilizing slicing

Strategy: To unravel the issue comply with the beneath steps:

  • Initialize a variable ‘n’ to the specified size of the substrings.
  • Use a for loop to iterate via the characters of the unique string, ranging from the primary character.
  • In every iteration of the loop, use slicing to extract a substring of size ‘n’ from the unique string, ranging from the present character. The slicing will be finished by utilizing the syntax ‘string[start:end]’ the place the beginning and finish are the indices of the primary and final character of the substring, respectively.
  • In every iteration, the variable ‘i’ would be the beginning index of the substring, and ‘i + n’ would be the ending index of the substring, so the substring will be extracted by utilizing the slicing syntax ‘string[i: i + n]’.
  • Print or retailer the extracted substring for additional processing.
  • Repeat the method for all characters within the authentic string.

Under is the implementation of the above strategy:

C++

#embrace <iostream>

#embrace <string.h>

utilizing namespace std;

  

int important()

{

    string str = "geeksforgeeks";

    int n = 3;

    for (int i = 0; i < str.size() - n + 1; i++)

        cout << str.substr(i, n) << " ";

    return 0;

}

Java

import java.util.*;

  

class GFG {

    public static void important(String args[])

    {

        String str = "geeksforgeeks";

        int n = 3;

        for (int i = 0; i < str.size() - n + 1; i++)

            System.out.print(str.substring(i, i + n) + " ");

    }

}

Python3

str = "geeksforgeeks"

n = 3

  

for i in vary(len(str) - n + 1):

    print(str[i:i + n], finish = " ")

  

C#

utilizing System;

  

class MainClass {

    public static void Foremost(string[] args)

    {

        string str = "geeksforgeeks";

        int n = 3;

        for (int i = 0; i < str.Size - n + 1; i++)

            Console.Write(str.Substring(i, n) + " ");

    }

}

  

Javascript

<script>

let str = "geeksforgeeks";

let n = 3;

for (let i = 0; i < str.size - n + 1; i++) {

    let substring = str.substring(i, i+n);

    doc.write(substring+" ");

}

  

</script>

Output

gee eek eks ksf sfo for org rge gee eek eks 

Time Complexity: O(|str|*n), the place |str| is the size of the string
Auxiliary Area: O(n)

Technique 2: Utilizing a for loop

Strategy: To unravel the issue comply with the beneath steps:

  • Initialize a variable ‘n‘ to the specified size of the substrings.
  • Use a for loop to iterate via the characters of the unique string, ranging from the primary character.
  • In every iteration of the outer loop, initialize an empty variable ‘substring‘ to retailer the extracted substring.
  • Use a nested for loop to iterate via the following ‘n’ characters of the unique string, ranging from the present character.
  • In every iteration of the interior loop, add the present character to the ‘substring’ variable.
  • After the interior loop finishes, print or retailer the extracted substring for additional processing.
  • Repeat the method for all characters within the authentic string.

Under is the implementation of the above strategy:

C++

#embrace <iostream>

#embrace <string.h>

utilizing namespace std;

  

int important()

{

    string str = "geeksforgeeks";

    int n = 3;

    for (int i = 0; i < str.size() - n + 1; i++) {

        string substring = "";

        for (int j = i; j < i + n; j++)

            substring += str[j];

        cout << substring << " ";

    }

    return 0;

}

Java

import java.util.*;

  

public class GFG {

    public static void important(String[] args)

    {

        String str = "geeksforgeeks";

        int n = 3;

        for (int i = 0; i < str.size() - n + 1; i++) {

            String substring = "";

            for (int j = i; j < i + n; j++)

                substring += str.charAt(j);

            System.out.print(substring + " ");

        }

    }

}

  

Python3

str = "geeksforgeeks"

n = 3

  

for i in vary(len(str) - n + 1):

    substring = ""

    for j in vary(i, i + n):

        substring += str[j]

    print(substring, finish =' ')

  

C#

utilizing System;

  

class Program {

    static void Foremost(string[] args)

    {

        string str = "geeksforgeeks";

        int n = 3;

        for (int i = 0; i < str.Size - n + 1; i++) {

            string substring = "";

            for (int j = i; j < i + n; j++)

                substring += str[j];

            Console.Write(substring + " ");

        }

    }

}

  

Javascript

<script>

var str = "geeksforgeeks";

var n = 3;

for (let i = 0; i < str.size - n + 1; i++) {

    var substring = str[i];

    for (let j = i+1; j < i + n; j++)

        substring += str[j];

    doc.write(substring + " ");

}

  

</script>

Output

gee eek eks ksf sfo for org rge gee eek eks 

Time Complexity: O(|str|*n), the place |str| is the size of the string
Auxiliary Area: O(n)

Technique 3: Utilizing record comprehension (Just for Python)

Strategy: To unravel the issue comply with the beneath steps:

  • Initialize a variable ‘n’ to the specified size of the substrings.
  • Use an inventory comprehension to iterate via the characters of the unique string and extract substrings of size ‘n’ in a single line.
  • The record comprehension makes use of the slicing syntax ‘string[start:end]’ the place the beginning and finish are the indices of the primary and final character of the substring, respectively.
  • The variable ‘i’ would be the beginning index of the substring and ‘i+n’ would be the ending index of the substring, so the substring will be extracted by utilizing the slicing syntax ‘string[i:i+n]’ contained in the record comprehension.
  • Assign the output of the record comprehension to a variable e.g. substrings = [string[i:i+n] for i in vary(len(string) – n + 1)]
  • Then you may print or entry particular person substrings from the record by indexing.

Under is the implementation of the above strategy:

Python3

str = "geeksforgeeks"

n = 3

substrings = [str[i:i + n] for i in vary(len(str) - n + 1)]

print(substrings)

  

Output

['gee', 'eek', 'eks', 'ksf', 'sfo', 'for', 'org', 'rge', 'gee', 'eek', 'eks']

Time Complexity: O(|str|*n), the place |str| is the size of the string
Auxiliary Area: O(|str|*n)

N.B.: You will need to notice that the above strategy will solely work if the size of the string is larger than n, in any other case it’s going to throw index out of vary error.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

situs slot gacor provider terbaik agen toto slot terpercaya 2023 agen toto togel terpercaya 2023 situs toto togel pasaran resmi terbaik bandar toto macau pasaran resmi toto togel bandar toto slot gacor 4d 2023 bo togel online pasaran terlengkap sepanjang masa bo toto slot terlengkap sepanjang masa situs toto togel 2023 bet 100 perak daftar toto slot dan toto togel 2023 bermain toto togel dengan bet hanya 100 perak daftar toto slot bonus new member terpercaya bermain toto slot pelayanan 24 jam nonstop agen slot gacor 4d hadiah terbesar bandar toto slot provider terbaik toto slot gacor 4d hingga toto togel toto togel pasaran resmi terpercaya bo togel online terbaik 2023 agen togel online terbesar 2023 situs togel online terpercaya 2023 bo togel online paling resmi 2023 toto togel pasaran togel hongkong resmi situs slot online pasti gacor agen slot online anti rungkad bo slot online deposit tanpa potongan situs toto togel dan toto slot bonus new member situs toto slot gacor 4d bo toto slot gacor 4d bo toto slot gacor dari toto togel 4d bo toto slot 4d terpercaya bo toto slot terpercaya toto macau resmi dari toto togel 4d agen togel terbesar dan situs toto slot terpercaya bandar toto togel dan slot online 2023 bo slot gacor terbaik sepanjang masa winsortoto winsortoto bo toto togel situs toto situs toto togel terpercaya situs toto slot terpercaya situs slot gacor 4d terbaik sepanjang masa agen toto togel dan situs toto slot terpercaya situs toto togel dan agen toto slot terpercaya bandar toto togel tersedia pasaran toto macau resmi agen toto togel bet 100 perak deposit 10rb ltdtoto