Skip to content

Improve index_of with KMP algorithm #33

@davidgaspardev

Description

@davidgaspardev

Description

Replace the current naive O(n×m) search in index_of with the Knuth-Morris-Pratt (KMP) algorithm, which runs in O(n+m) time by preprocessing the pattern to avoid redundant comparisons.

Current complexity

Naive: O(n × m)  — n = target length, m = fragment length

Target complexity

KMP:   O(n + m)  — linear time

How KMP works

  1. Build a failure function (partial match table) from the pattern — O(m)
  2. Use it to skip positions in the target that are guaranteed not to match — O(n)

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions