Welcome to Day 53 of the #80DaysOfChallenges journey! This intermediate challenge finally exposes one of Python's most surprising gotchas: str.count() does NOT count overlapping occurrences. Want to count how many times "aa" appears in "aaa"? str.count() says 1, but the correct answer is 2. This challenge fixes that with a clean, manual sliding window that correctly counts overlaps in O(n) time.
Real-world use cases: DNA sequence analysis, text processing, log parsing, search engines, and anywhere overlaps matter.
Example:
text = "aaaa", pattern = "aa" → returns 3 ("aa" at positions 0-1, 1-2, 2-3)
If you're tired of str.count() giving wrong answers, this "Python overlapping substring count" solution is the reliable, readable fix you've been looking for.
💡 Key Takeaways from Day 53: Overlapping Substring Counter
This task delivers a bulletproof function that slides a window of pattern length across the text and counts every match, including overlaps. We'll break it down: early validation for edge cases, sliding window with slicing, and interactive main.
1. Smart Validation: Handle Impossible Cases First
def count_overlapping_substrings(text: str, pattern: str) -> int:
if not pattern: # avoid infinite/ambiguous matches
return 0
n, m = len(text), len(pattern)
if m > n: # pattern longer than text -> zero matches
return 0
Empty pattern? Return 0 (prevents infinite loop disaster).
Pattern longer than text? Instant 0.
These guards make the function safe and fast.
2. Sliding Window Core: The Real Overlap Magic
count = 0
for i in range(0, n - m + 1): # every possible starting position
if text[i : i + m] == pattern:
count += 1
return count
The range n - m + 1 gives every valid starting index.
Slicing text[i:i+m] is Pythonic and fast.
Step is 1, so overlaps naturally counted.
For "aaaa" and "aa": checks positions 0,1,2 → all match → count=3.
This is the standard way to count overlaps when built-ins fail.
3. Main Interactive: Clean Input and Output
text_input = input("Enter the text: ").rstrip("\n")
pattern_input = input("Enter the pattern to search for: ").rstrip("\n")
if pattern_input == "":
print("Pattern is empty. Returning 0.")
else:
occurrences = count_overlapping_substrings(text_input, pattern_input)
print(f"Overlapping occurrences of '{pattern_input}' in the text:", occurrences)
Handles empty pattern gracefully, rstrip for clean input, clear formatted output.
🎯 Summary and Reflections
This overlapping substring counter fixes Python's biggest string surprise with just a few lines of crystal-clear code. It reinforced:
- str.count() limitation: It steps by len(pattern), missing overlaps.
- Sliding window universality: Same pattern used in KMP, Rabin-Karp, DNA matching.
- Slicing power: text[i:i+m] is readable and efficient.
Reflections: This exact problem appears in bioinformatics (motif counting), text editors, and search algorithms. Now you know why some libraries have "overlapping=True" options.
Advanced Alternatives:
- Use str.find in loop with step 1
- Regex with lookahead: len(re.findall(f'(?={re.escape(pattern)})', text))
- Built-in in newer Python? No, still need manual!
But this slicing version is fastest and most readable.
🚀 Next Steps and Resources
Day 53 destroyed a Python myth and gave you a weapon for real string problems.
- Source Code for Challenge #53: scripts/count_sub_overlaps.py
- Main Repository: 80-days-of-challenges
- Daily Updates: Twitter/X (@Shahrouzlogs)
Try "aaaaaaa" with "aaa", how many overlaps? Answer below! 🔥
Top comments (0)