Python – Extract K length substrings
There are many problems in which we require to get all K length substrings of a string. This particular utility is very popular in competitive programming and having shorthands to solve this problems can always be handy. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using list comprehension + string slicing The combination of list comprehension and string slicing can be used to perform this particular task. This is just brute force method to perform this task.
Python3
# Python3 code to demonstrate working of # Extract K length substrings # Using list comprehension + string slicing # initializing string test_str = "Geeks" # printing original string print ( "The original string is : " + str (test_str)) # initializing K K = 3 # Extract K length substrings # Using list comprehension + string slicing res = [test_str[i: j] for i in range ( len (test_str)) for j in range (i + 1 , len (test_str) + 1 ) if len (test_str[i:j]) = = K] # printing result print ( "All K length substrings of string are : " + str (res)) |
The original string is : Geeks All K length substrings of string are : ['Gee', 'eek', 'eks']
Time Complexity: O(n2)
Space Complexity: O(n)
Method #2 : Using itertools.combinations() This particular task can also be performed using the inbuilt function of combinations, which helps to get all K length the possible combinations i.e the substrings from a string.
Python3
# Python3 code to demonstrate working of # Extract K length substrings # Using itertools.combinations() from itertools import combinations # initializing string test_str = "Geeks" # printing original string print ( "The original string is : " + str (test_str)) # initializing K K = 3 # Extract K length substrings # Using itertools.combinations() res = [test_str[x:y] for x, y in combinations( range ( len (test_str) + 1 ), r = 2 ) if len (test_str[x:y]) = = K ] # printing result print ( "All K length substrings of string are : " + str (res)) |
The original string is : Geeks All K length substrings of string are : ['Gee', 'eek', 'eks']
Time Complexity: O(n2) -> (loop+combinations)
Space Complexity: O(n)
Method #3: Using a for loop
Step-by-step approach:
- Initialize an empty list to store the K length substrings
- Loop through the range from 0 to the length of the string minus K (i.e. up to the last possible starting index for a substring of length K)
- For each index i in the range, append the substring starting at index i and ending at i+K to the list
- Print the list of K length substrings
Below is the implementation of the above approach:
Python3
test_str = "Geeks" print ( "The original string is : " + str (test_str)) K = 3 # Extract K length substrings using for loop res = [] for i in range ( len (test_str) - K + 1 ): res.append(test_str[i:i + K]) print ( "All K length substrings of string are : " + str (res)) |
The original string is : Geeks All K length substrings of string are : ['Gee', 'eek', 'eks']
Time complexity: O(nK), where n is the length of the string and K is the length of the desired substrings.
Auxiliary space: O(nK), as the list of substrings can potentially hold n/K elements, each with K character
Please Login to comment...