Python - Number of positions where Substrings Match of Length K

In string processing, we often need to find how many times a substring of specific length appears at different positions in a string. This tutorial demonstrates how to count the number of positions where substrings of length K match a given pattern using Python.

Understanding the Problem

Given an input string, we need to find how many positions contain a specific substring of length K. For example, if we search for "aaab" (K=4) in the string "aaabddhaaabnsnsaaabkskd", we need to count all occurrences at different positions.

Input String: aaabddhaaabnsnsaaabkskd Pattern: aaab (K=4) Position 0: aaab Position 7: aaab Position 15: aaab Total Matches: 3

Algorithm

The approach involves sliding a window of size K across the input string and comparing each substring with the target pattern:

  • Step 1 Initialize the input string, length K, and target substring

  • Step 2 Create a function that takes the input string, K, and substring as parameters

  • Step 3 Initialize a counter variable to track matches

  • Step 4 Iterate through the string from index 0 to (length - K + 1)

  • Step 5 Extract substring of length K at each position

  • Step 6 Compare extracted substring with target pattern and increment counter if they match

  • Step 7 Return the final count

Implementation

def count_substring_positions(input_str, K, target_substr):
    """
    Count the number of positions where substrings of length K match the target.
    
    Args:
        input_str: The input string to search in
        K: Length of substring to extract
        target_substr: The substring pattern to match
        
    Returns:
        Number of matching positions
    """
    counter = 0
    
    # Iterate through valid starting positions
    for i in range(len(input_str) - K + 1):
        # Extract substring of length K starting at position i
        current_substr = input_str[i:i+K]
        
        # Check if it matches our target
        if current_substr == target_substr:
            counter += 1
            
    return counter

# Example usage
input_str = "aaabddhaaabnsnsaaabkskd"
K = 4
target_substr = "aaab"

result = count_substring_positions(input_str, K, target_substr)
print(f"The substring '{target_substr}' with length {K} appears at {result} positions")

# Show the actual positions
print("\nPositions found:")
for i in range(len(input_str) - K + 1):
    if input_str[i:i+K] == target_substr:
        print(f"Position {i}: '{input_str[i:i+K]}'")
The substring 'aaab' with length 4 appears at 3 positions

Positions found:
Position 0: 'aaab'
Position 7: 'aaab'
Position 15: 'aaab'

Alternative Approach Using List Comprehension

For a more concise solution, we can use list comprehension:

def count_positions_compact(input_str, K, target_substr):
    """Count matching positions using list comprehension."""
    return sum(1 for i in range(len(input_str) - K + 1) 
               if input_str[i:i+K] == target_substr)

# Example
input_str = "abcabcabcabc"
K = 3
target_substr = "abc"

count = count_positions_compact(input_str, K, target_substr)
print(f"'{target_substr}' appears {count} times in '{input_str}'")
'abc' appears 4 times in 'abcabcabcabc'

Time and Space Complexity

Metric Complexity Description
Time O(N × K) N iterations, each comparing K characters
Space O(K) Space for substring extraction

Where N is the length of the input string and K is the substring length.

Conclusion

This sliding window approach efficiently counts substring matches by iterating through valid positions and comparing extracted substrings. The algorithm has O(N × K) time complexity and is suitable for most practical applications involving pattern matching in strings.

Updated on: 2026-03-27T15:36:03+05:30

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements