Skip to content

Add Suffix Automaton algorithm in strings - #15082

Closed
Clear20-22 wants to merge 1 commit into
TheAlgorithms:masterfrom
Clear20-22:add-suffix-automaton
Closed

Add Suffix Automaton algorithm in strings#15082
Clear20-22 wants to merge 1 commit into
TheAlgorithms:masterfrom
Clear20-22:add-suffix-automaton

Conversation

@Clear20-22

Copy link
Copy Markdown
Contributor

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 14:40
@algorithms-keeper algorithms-keeper Bot added the require descriptive names This PR needs descriptive function and/or variable names label Aug 25, 2026

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

occurrences = [0] * len(self.states)
order = sorted(
range(len(self.states)),
key=lambda i: self.states[i].length,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: i

@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 pull request adds a new string-processing algorithm module implementing a Suffix Automaton (SAM), enabling substring membership checks, distinct-substring counting, and substring occurrence counting over a fixed input string.

Changes:

  • Add strings/suffix_automaton.py implementing SAM construction and core query operations.
  • Include doctests demonstrating contains, count_distinct_substrings, count_occurrences, and input validation for empty strings.

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

Comment on lines +46 to +63
def __init__(self, string: str) -> None:
if not string:
raise ValueError("Input string must not be empty.")

self.states: list[State] = [State(length=0, link=-1)]
self.last: int = 0
self.string: str = string

for char in string:
self.extend(char)

def extend(self, char: str) -> None:
"""
Extend the Suffix Automaton by appending character char.
Time Complexity: O(1) amortized
"""
curr = len(self.states)
self.states.append(State(length=self.states[self.last].length + 1))
Comment on lines +127 to +140
def count_occurrences(self, pattern: str) -> int:
"""
Count occurrences of pattern as a substring in the text in O(|pattern|) time.

>>> sam = SuffixAutomaton("banana")
>>> sam.count_occurrences("an")
2
>>> sam.count_occurrences("na")
2
>>> sam.count_occurrences("banana")
1
>>> sam.count_occurrences("xyz")
0
"""
@Clear20-22 Clear20-22 closed this Aug 25, 2026
@Clear20-22 Clear20-22 reopened this Aug 25, 2026

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

occurrences = [0] * len(self.states)
order = sorted(
range(len(self.states)),
key=lambda i: self.states[i].length,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: i

@Clear20-22 Clear20-22 closed this Aug 25, 2026
@Clear20-22

Copy link
Copy Markdown
Contributor Author

This is not the final commit

@Clear20-22
Clear20-22 deleted the add-suffix-automaton branch August 25, 2026 14:51
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 require descriptive names This PR needs descriptive function and/or variable names

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants