Python – Number of positions where Substrings Match of Length K
Last Updated :
19 Mar, 2023
Given 2 Strings, count positions where substrings of Length K Match.
Input : test_str1 = ‘geeksforgeeks’, test_str2 = ‘peeksformeeks’, K = 4
Output : 4
Explanation : 4 positions match K length Substrings.
Input : test_str1 = ‘geeksforgeeks’, test_str2 = ‘peeksformeeks’, K = 5
Output : 3
Explanation : 3 positions match K length Substrings.
Method #1 : Using list comprehension + min() + slicing
In this, we iterate through strings till length of minimum string, and the comparison is done by slicing using string slicing. Iteration is done through loop inside list comprehension.
Python3
test_str1 = 'geeksforgeeks'
test_str2 = 'peeksformeeks'
print ( "The original string 1 is : " + str (test_str1))
print ( "The original string 2 is : " + str (test_str2))
K = 3
res = len ([test_str1[idx : idx + K] for idx in range ( min ( len (test_str1), len (test_str2)) - K - 1 )
if test_str1[idx : idx + K] = = test_str2[idx : idx + K]])
print ( "Number of positions of matching K length Substrings : " + str (res))
|
Output
The original string 1 is : geeksforgeeks
The original string 2 is : peeksformeeks
Number of positions of matching K length Substrings : 5
Method #2 : Using map() + list comprehension
In this, we extract all K length substrings of one string and then using in operator and map, check for each substring if present in other String, this ignores the positional factor of problem.
Python3
test_str1 = 'geeksforgeeks'
test_str2 = 'peeksformeeks'
print ( "The original string 1 is : " + str (test_str1))
print ( "The original string 2 is : " + str (test_str2))
K = 3
subs_str = [test_str1[idx : idx + K] for idx in range ( len (test_str1) - K - 1 )]
res = list ( map ( lambda ele: ele in test_str2, subs_str)).count( True )
print ( "Number of positions of matching K length Substrings : " + str (res))
|
Output
The original string 1 is : geeksforgeeks
The original string 2 is : peeksformeeks
Number of positions of matching K length Substrings : 5
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using set intersection and zip function
Step by step Algorithm:
- Initialize the strings and the length of the matching substrings (K).
- Create two sets using the zip() function to get tuples of (character in test_str1, character in test_str2) for both strings, where the tuples are created for the indices up to K-1.
- Get the set difference between the two sets obtained in step 2 to get all the tuples of matching substrings.
- Count the number of elements in the resulting set.
- Subtract 1 from the count obtained in step 4 to exclude the overlap at the start of the strings.
- Return the count as the final result.
Python3
test_str1 = 'geeksforgeeks'
test_str2 = 'peeksformeeks'
print ( "The original string 1 is : " + str (test_str1))
print ( "The original string 2 is : " + str (test_str2))
K = 3
res = len ( set ( zip (test_str1, test_str2)) - set ( zip (test_str1[:K - 1 ], test_str2[:K - 1 ])))
res = res - 1 ;
print ( "Number of positions of matching K length Substrings : " + str (res))
|
Output
The original string 1 is : geeksforgeeks
The original string 2 is : peeksformeeks
Number of positions of matching K length Substrings : 9
Time complexity: O(n), where n is the length of the shortest string.
Space complexity: O(n), where n is the length of the shortest string.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...