Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Non-Overlapping occurrences of N Repeated K character
In this article, we'll find the non-overlapping occurrences of N repeated K characters using Python. This is a common string processing problem where we need to count how many times a specific character appears consecutively a given number of times.
Understanding the Problem
Given a string, we need to find non-overlapping occurrences where character K appears exactly N consecutive times. For example, in string "AABBCCAAA", if K="A" and N=2, we look for "AA" patterns that don't overlap.
Algorithm
Step 1 Define a function that takes character K, repetition count N, and input string
Step 2 Initialize a counter for non-overlapping occurrences
Step 3 Iterate through the string to find consecutive occurrences of character K
Step 4 When we find N consecutive occurrences, increment counter and skip ahead to avoid overlap
Step 5 Return the total count
Method 1: Using String Iteration
This approach iterates through the string and counts consecutive occurrences ?
def non_overlapping_occurrences(K, N, the_str):
result = 0
i = 0
while i < len(the_str):
# Check if we have N consecutive K characters
if the_str[i:i+N] == K * N:
result += 1
i += N # Skip N characters to avoid overlap
else:
i += 1
return result
# Example usage
the_str = 'AABBCCAAA'
K = "A"
N = 2
print("Input String:", the_str)
print("Character:", K, "| Repetition:", N)
output = non_overlapping_occurrences(K, N, the_str)
print("Non-overlapping occurrences:", output)
Input String: AABBCCAAA Character: A | Repetition: 2 Non-overlapping occurrences: 2
Method 2: Using Regular Expressions
We can use regex to find all occurrences and then filter for non-overlapping ones ?
import re
def non_overlapping_regex(K, N, the_str):
pattern = K * N # Create pattern like "AA"
result = 0
start = 0
while start < len(the_str):
match = re.search(pattern, the_str[start:])
if match:
result += 1
start += match.start() + N # Move past this match
else:
break
return result
# Example usage
the_str = 'AABBCCAAA'
K = "A"
N = 2
print("Input String:", the_str)
output = non_overlapping_regex(K, N, the_str)
print("Non-overlapping occurrences:", output)
Input String: AABBCCAAA Non-overlapping occurrences: 2
Method 3: Using Split Approach
This approach splits the string and counts patterns in each segment ?
def non_overlapping_split(K, N, the_str):
# Replace the target pattern with a delimiter
pattern = K * N
segments = the_str.split(pattern)
# Count how many splits occurred (number of patterns found)
return len(segments) - 1
# Example usage
test_cases = [
('AABBCCAAA', 'A', 2),
('AAABBBAAACCC', 'A', 3),
('ABABAB', 'AB', 2)
]
for the_str, K, N in test_cases:
result = non_overlapping_split(K, N, the_str)
print(f"String: {the_str} | Pattern: {K*N} | Count: {result}")
String: AABBCCAAA | Pattern: AA | Count: 2 String: AAABBBAAACCC | Pattern: AAA | Count: 2 String: ABABAB | Pattern: ABAB | Count: 1
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| String Iteration | O(m) | O(1) | Simple cases, memory-efficient |
| Regular Expressions | O(m) | O(1) | Complex patterns |
| Split Approach | O(m) | O(m) | Quick counting without iteration |
Conclusion
All three methods efficiently find non-overlapping occurrences with O(m) time complexity. The string iteration method is most memory-efficient, while the split approach offers the simplest implementation for basic pattern counting.
