Add Suffix Automaton algorithm in strings - #15082
Conversation
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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, |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: i
There was a problem hiding this comment.
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.pyimplementing 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.
| 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)) |
| 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 | ||
| """ |
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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, |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: i
|
This is not the final commit |
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:
contains).count_distinct_substrings).count_occurrences).Checklist: