Skip to content
Back to skills

Algo Naive Pattern Matching

ASecurity

Naive O(nm) brute-force string matching — sliding window baseline, when it's acceptable, motivation for KMP/Rabin-Karp

  • 4 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added May 27, 2026
data-aipythongo

Security analysis

A100/100

Scanned May 27, 2026

npx -y skills add Pavel-Kravchenko/Bioinformatics --skill algo-naive-pattern-matching --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Algo Naive Pattern Matching?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Algo Naive Pattern Matching
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/pavel-kravchenko-algo-naive-pattern-matching/badge)](https://www.skillsdirectory.com/skills/pavel-kravchenko-algo-naive-pattern-matching)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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")

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.