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 ` `#embrace ` `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

 ``
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 ` `#embrace ` `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

 ``
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