Naive O(nm) brute-force string matching — sliding window baseline, when it's acceptable, motivation for KMP/Rabin-Karp
Scanned 5/27/2026
Install via CLI
openskills install Pavel-Kravchenko/Bioinformatics---
name: algo-naive-pattern-matching
description: "Naive O(nm) brute-force string matching — sliding window baseline, when it's acceptable, motivation for KMP/Rabin-Karp"
tool_type: python
primary_tool: Python
---
# Naive Pattern Matching
## Algorithm
Slide pattern over text, comparing character by character at each position.
```python
def naive_search(text: str, pattern: str) -> list[int]:
n, m = len(text), len(pattern)
positions = []
if m == 0 or m > n:
return positions
for i in range(n - m + 1):
match = True
for j in range(m):
if text[i + j] != pattern[j]:
match = False
break
if match:
positions.append(i)
return positions
```
## Complexity
| Case | Time | When |
|---|---|---|
| Best | O(n) | First char mismatches quickly |
| Worst | O(n x m) | Many partial matches (e.g., T="AAA...B", P="AA...B") |
| Average | O(n) for large alphabets | Random text, mismatches found early |
| Space | O(1) auxiliary | |
## When Naive is Fine
- **Small patterns:** m is small (e.g., word in a document), so O(nm) ~ O(n)
- **Large alphabet:** More characters means mismatches found quickly (average ~1-2 comparisons per position)
- **One-off search:** No preprocessing amortization needed
## Why It's Inefficient
After a partial match fails, it discards everything learned and restarts from scratch at the next position. KMP/Rabin-Karp/Boyer-Moore fix this.
| Algorithm | Time | Key Idea |
|---|---|---|
| KMP | O(n + m) | Failure function skips redundant comparisons |
| Rabin-Karp | O(n + m) avg | Rolling hash, O(1) window comparison |
| Boyer-Moore | O(n/m) best | Bad character rule skips large portions |
## Pitfalls
- Finds overlapping matches by default — this is correct behavior
- Python `str.find()` / `in` uses a mix of Boyer-Moore and Horspool internally and is faster than hand-written naive search
- Worst case is triggered by low-entropy text (e.g., DNA with small alphabet "ACGT")
No comments yet. Be the first to comment!
End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches.
Orchestrate multi-phase deep research with web search, memory retrieval, pattern matching, and synthesis into structured findings
Persistent memory systems for LLM conversations including short-term, long-term, and entity-based memory Use when: conversation memory, remember, memory persistence, long-term memory, chat history.
CodeTour `.tour`ファイルを作成 — ペルソナターゲット、ステップバイステップウォークスルー(実際のファイルとラインアンカー付き)。オンボーディングツアー、アーキテクチャウォークスルー、PRツアー、RCAツアー、構造化「これがどのように機能するかを説明」リクエストに使用。
KMPプロジェクト向けのCompose MultiplatformおよびJetpack Composeパターン — 状態管理、ナビゲーション、テーマ設定、パフォーマンス、プラットフォーム固有のUI。