-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Add Z-function algorithm implementation #2067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb97daa
Add Z-function algorithm implementation
nikalosa d2f4f05
Spelling correction
nikalosa 9d70fb8
Reference url correction
nikalosa 409521c
Add additional function as an example of z-function usage, change doc…
nikalosa c802884
Fix flake8 errors
nikalosa 4ae7614
Update z_function.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """ | ||
| https://cp-algorithms.com/string/z-function.html#:~:text=The%20Z%2Dfunction%20for%20this,Note. | ||
|
|
||
| For given string this algorithm computes value for each index, | ||
| which represents the maximal length substring starting from the index | ||
| and is same as the prefix of the same size | ||
|
|
||
| e.x. for string 'abab' for second index value would be 2 | ||
|
|
||
| The main adventage of the algorithm is that it's linear, using dynamic programming | ||
|
|
||
| Time Complexity: O(n) - where n is the length of the string | ||
|
|
||
|
|
||
| For the value of the first element the algorithm always returns 0 | ||
|
|
||
| """ | ||
|
|
||
|
|
||
| def z_function(input_str: str) -> list: | ||
| """ | ||
| Will convert the entire string to uppercase letters | ||
|
nikalosa marked this conversation as resolved.
Outdated
|
||
|
|
||
| >>> z_function("abracadabra") | ||
| [0, 0, 0, 1, 0, 1, 0, 4, 0, 0, 1] | ||
| >>> z_function("aaaa") | ||
| [0, 3, 2, 1] | ||
| >>> z_function("zxxzxxz") | ||
| [0, 0, 0, 4, 0, 0, 1] | ||
|
|
||
| """ | ||
|
|
||
| z_result = [0] * len(input_str) | ||
|
|
||
| # initialize interval's left pointer and right pointer | ||
| left_pointer, right_pointer = 0, 0 | ||
|
|
||
| for i in range(1, len(input_str)): | ||
| # case when current index is inside the interval | ||
| if i <= right_pointer: | ||
| min_edge = min(right_pointer - i + 1, z_result[i - left_pointer]) | ||
| z_result[i] = min_edge | ||
|
|
||
| while go_next(i, z_result, input_str): | ||
| z_result[i] += 1 | ||
|
|
||
| # if new index's result gives us more right interval, we've to update left_pointer and right_pointer | ||
|
nikalosa marked this conversation as resolved.
Outdated
|
||
| if i + z_result[i] - 1 > right_pointer: | ||
| left_pointer, right_pointer = i, i + z_result[i] - 1 | ||
|
|
||
| return z_result | ||
|
|
||
|
|
||
| # helping function which checks if following elements are equal or not | ||
| def go_next(i, z_result, s): | ||
|
nikalosa marked this conversation as resolved.
|
||
| return i + z_result[i] < len(s) and s[z_result[i]] == s[i + z_result[i]] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
| doctest.testmod() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.