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
# Python3 code to demonstrate working of
# Number of positions where Substrings Match of Length K
# Using list comprehension + min() + slicing
# initializing strings
test_str1 = 'geeksforgeeks'
test_str2 = 'peeksformeeks'
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
# initializing K
K = 3
# checking for substrings,
# using len() to get total count
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]])
# printing result
print("Number of positions of matching K length Substrings : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Number of positions where Substrings Match of Length K
# Using map() + list comprehension
# initializing strings
test_str1 = 'geeksforgeeks'
test_str2 = 'peeksformeeks'
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
# initializing K
K = 3
# Extracting Substrings
subs_str = [test_str1[idx : idx + K] for idx in range(len(test_str1) - K - 1)]
# checking in other string
# using count() to get number
res = list(map(lambda ele: ele in test_str2, subs_str)).count(True)
# printing result
print("Number of positions of matching K length Substrings : " + str(res))
OutputThe 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'
# printing original strings
print("The original string 1 is : " + str(test_str1))
print("The original string 2 is : " + str(test_str2))
# initializing K
K = 3
# using set intersection and zip function to count
res = len(set(zip(test_str1, test_str2)) - set(zip(test_str1[:K-1], test_str2[:K-1])))
res = res-1;
# printing result
print("Number of positions of matching K length Substrings : " + str(res))
OutputThe 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.
Similar Reads
Python - Longest Substring Length of K Given a String and a character K, find longest substring length of K. Input : test_str = 'abcaaaacbbaa', K = b Output : 2 Explanation : b occurs twice, 2 > 1. Input : test_str = 'abcaacccbbaa', K = c Output : 3 Explanation : Maximum times c occurs is 3. Method #1: Using loop This is brute way to
7 min read
Python program to find the character position of Kth word from a list of strings Given a list of strings. The task is to find the index of the character position for the word, which lies at the Kth index in the list of strings. Examples: Input : test_list = ["geekforgeeks", "is", "best", "for", "geeks"], K = 21 Output : 0Explanation : 21st index occurs in "geeks" and point to "g
3 min read
Python - Ways to Count Number of Substring in String Given a string s, determine the number of substrings that satisfy certain criteria. For example we are given a string s="hellohellohello" we need to count how many time the substring occur in the given string suppose we need to find the substring "hello" so that the count becomes 3. We can use metho
2 min read
Python program to get all possible slices of a string for K number of slices Given a string, the task is to write a Python program to get all possible slices for K number of slices. Input : test_str = "Gfg4all", K = 3Output : [['G', 'f', 'g4all'], ['G', 'fg', '4all'], ['G', 'fg4', 'all'], ['G', 'fg4a', 'll'], ['G', 'fg4al', 'l'], ['Gf', 'g', '4all'], ['Gf', 'g4', 'all'], ['G
4 min read
Find the Index of a Substring in Python Finding the position of a substring within a string is a common task in Python. In this article, we will explore some simple and commonly used methods to find the index of a substring in Python.Using str.find() The find() method searches for the first occurrence of the specified substring and return
3 min read