Sitelet https://github.com/TheAlgorithms/Python/pull/15083
Skip to content

Add suffix automaton - #15083

Open
Clear20-22 wants to merge 2 commits into
TheAlgorithms:masterfrom
Clear20-22:add-suffix-automaton
Open

Add suffix automaton#15083
Clear20-22 wants to merge 2 commits into
TheAlgorithms:masterfrom
Clear20-22:add-suffix-automaton

Conversation

@Clear20-22

Copy link
Copy Markdown

Describe your change:

This pull request implements the Suffix Automaton (SAM) data structure and algorithm in strings/suffix_automaton.py.

A Suffix Automaton is the minimal Deterministic Finite Automaton (DFA) that recognizes all suffixes and substrings of a given string in $O(N)$ time and $O(N)$ space.

Key features implemented:

  • $O(1)$ amortized extension / $O(N)$ construction.
  • $O(|pattern|)$ substring search (contains).
  • $O(N)$ distinct substrings count (count_distinct_substrings).
  • $O(|pattern|)$ substring occurrence count (count_occurrences).
  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Copilot AI lite review requested due to automatic review settings August 25, 2026 17:24
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Aug 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new string-processing algorithm module implementing a Suffix Automaton (SAM) to support fast substring queries on a fixed input string.

Changes:

  • Introduces strings/suffix_automaton.py implementing SAM construction with incremental extension.
  • Adds substring membership checking (contains) and substring counting utilities (count_distinct_substrings, count_occurrences).
  • Includes doctests and a __main__ doctest runner for the new module.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +131 to +133
"""
Count occurrences of pattern as a substring in the text in O(|pattern|) time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants