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++
|
Java
|
Python3
|
C#
|
Javascript
|
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++
|
Java
|
Python3
|
C#
|
Javascript
|
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
|
['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.