Python Program to split string into k sized overlapping strings
Last Updated :
22 Apr, 2023
Given a string, the task is to write a Python program to extract overlapping consecutive string slices from the original string according to size K.
Example:
Input : test_str = ‘Geeksforgeeks’, K = 4
Output : [‘Geek’, ‘eeks’, ‘eksf’, ‘ksfo’, ‘sfor’, ‘forg’, ‘orge’, ‘rgee’, ‘geek’, ‘eeks’]
Explanation : Consecutive overlapping 4 sized strings are output.
Input : test_str = ‘Geeksforgeeks’, K = 6
Output : [‘Geeksf’, ‘eeksfo’, ‘eksfor’, ‘ksforg’, ‘sforge’, ‘forgee’, ‘orgeek’, ‘rgeeks’]
Explanation : Consecutive overlapping 6 sized strings are output.
Method 1: Using islice() + generator function + join()
In this, windows of size K are extracted using the islice(), and results are yielded in an intermediate way using yield. The final results are joined using join().
Python3
from itertools import islice
def over_slice(test_str, K):
itr = iter (test_str)
res = tuple (islice(itr, K))
if len (res) = = K:
yield res
for ele in itr:
res = res[ 1 :] + (ele,)
yield res
test_str = 'Geeksforgeeks'
print ( "The original string is : " + str (test_str))
K = 4
res = ["".join(ele) for ele in over_slice(test_str, K)]
print ( "Overlapping windows : " + str (res))
|
Output:
The original string is : Geeksforgeeks
Overlapping windows : [‘Geek’, ‘eeks’, ‘eksf’, ‘ksfo’, ‘sfor’, ‘forg’, ‘orge’, ‘rgee’, ‘geek’, ‘eeks’]
Method 2: Using list comprehension + slicing
In this example, intermediate slices are performed using a slicing operation in a more pythonic way. Each window is extracted using slice notation.
Python3
test_str = 'Geeksforgeeks'
print ( "The original string is : " + str (test_str))
K = 4
res = [test_str[idx:idx + K] for idx in range ( len (test_str) - K + 1 )]
print ( "Overlapping windows : " + str (res))
|
Output:
The original string is : Geeksforgeeks
Overlapping windows : [‘Geek’, ‘eeks’, ‘eksf’, ‘ksfo’, ‘sfor’, ‘forg’, ‘orge’, ‘rgee’, ‘geek’, ‘eeks’]
The Time and Space Complexity of all the methods is :
Time Complexity: O(n)
Space Complexity: O(n)
Method 3: Using recursion :
Python3
test_str = 'Geeksforgeeks'
print ( "The original string is : " + str (test_str))
K = 4
def over_slice(test_str, K, res = []):
if ( len (test_str) < K):
return
res.append(test_str[:K])
over_slice(test_str[ 1 :], K, res)
return res
res = over_slice(test_str, K)
print ( "Overlapping windows : " + str (res))
|
Output
The original string is : Geeksforgeeks
Overlapping windows : ['Geek', 'eeks', 'eksf', 'ksfo', 'sfor', 'forg', 'orge', 'rgee', 'geek', 'eeks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4 : using the windowed() function from the more_itertools module.
This function takes an iterable and a window size, and returns an iterator over overlapping windows of the specified size.
The steps for this approach are:
Import the windowed() function from the more_itertools module.
Initialize a list containing the string to be split.
Use the windowed() function to get an iterator over overlapping windows of size K.
Use a list comprehension to join each window of characters into a string.
Return the list of overlapping windows.
Python3
from more_itertools import windowed
test_str = 'Geeksforgeeks'
print ( "The original string is : " + str (test_str))
K = 4
str_list = list (test_str)
windows = windowed(str_list, K, step = 1 )
res = ["".join(w) for w in windows]
print ( "Overlapping windows : " + str (res))
|
OUTPUT :
The original string is : Geeksforgeeks
Overlapping windows : ['Geek', 'eeks', 'eksf', 'ksfo', 'sfor', 'forg', 'orge', 'rgee', 'geek', 'eeks']
The time complexity of this approach is O(N), since the windowed() function is a generator and only generates the windows as they are needed, without precomputing them all at once.
The auxiliary space complexity is O(NK), since we need to store all the overlapping windows in a list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...