Python - Number of positions where Substrings Match of Length K


In the given problem statement we have to find the number of positions on which substrings match of length K using Python programming. So this problem can be helpful to get all the K length substrings from the input string.

Understanding the Problem

The problem at hand requires an input string in which there are some substrings. So we have to show the number of positions of the substrings match in the given string. For example let’s see the below image −

In the above image we can see that the value of K is ‘aaab’ so after counting this string the Output will be 4 as there are four times the string is appearing in the input string.

Logic for The Above Problem

To solve this problem we will define the substring and the length of substring K. And then we will extract the substring from the given string and match with the defined string and if they both match then increment the count value. And at the end return the value of count to show the number of positions where substrings match of length K.

Algorithm

  • Step 1 − First initiate the variables for input string, value of K and the substring as input_str, K and substr.

  • Step 2 − Define the function to count the matching positions of the substring in the input string. And inside this function pass three arguments as input_str, K and substr.

  • Step 3 − Then initiate the variable called counter to keep record of the count of substrings.

  • Step 4 − A loop will be initiated over the index of the string until the length of string - K + 1. This will denote the substring size.

  • Step 5 − Inside the loop we will extract the K from the string.

  • Step 6 − Next, compare the extracted string with the given substring. And if they both are the same then we will increment the counter value by 1.

  • Step 7 − After iterating the full string we will return the final counter value to show on the console.

Example

# Initialize the input string
input_str = "aaabddhaaabnsnsaaabkskd"
K = 4
# Initialize the substring
substr = "aaab"
# Define the function to count the matching string
def count_positions(input_str, K, substr):
   # Initialize the counter with 0
   counter = 0
   # Iterate the input string
   for i in range(len(input_str) - K + 1):
      str_size = input_str[i:i+K]
      if str_size == substr:
         counter += 1
   return counter
# Call the above function
Output = count_positions(input_str, K, substr)
print(f"The substring '{substr}' with length {K} is matching at: '{Output}' positions")

Output

The substring 'aaab' with length 4 is matching at: '3' positions

Complexity

The time complexity for finding the number of positions where the substrings match of length K is O(N), here N is the length of the given input_str. As the code iterates over the string from 0 to len(input_str) - K +1.

Conclusion

As we have successfully solved the given problem using Python. We have used basic Python functionality to get the efficient solution to get the number of positions of the substrings.

Updated on: 17-Oct-2023

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements