Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.8k
algorithm: ZFunction#1239
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
algorithm: ZFunction#1239
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
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,41 @@ | ||
| /** | ||
| * @author: Adrito Mukherjee | ||
| * Implementation of ZFunction in JavaScript | ||
| * ZFunction at an index i gives the length of the longest substring starting at i, that is also a prefix of the whole string | ||
| * ZFunction for all indices in a string can be calculated in O(N) | ||
| * @see https://cp-algorithms.com/string/z-function.html | ||
| * @param {String} text The string whose Z Function is to be calculated | ||
| * @return {Array} Returns an array whose i-th index is the value of Z Function for text at index i | ||
| */ | ||
| function zFunction (text) { | ||
| const length = text.length | ||
| const zArray = Array(length).fill(0) | ||
| // Initializing left and right variable to zero | ||
| let left = 0 | ||
| let right = 0 | ||
| for (let index = 0; index < length; index++) { | ||
| // If index is less than or equal to right, we reuse the values of zFunction at index right-index+1 | ||
| // It is made sure that value of zFunction at index is not greater than maximum possible value at index | ||
| if (index <= right) { | ||
| zArray[index] = Math.min(right - index + 1, zArray[index - left]) | ||
| } | ||
| // After zArray[index] is initialized, we see if we can increase its value by trivially comparing character by character | ||
| while ( | ||
| index + zArray[index] < length && | ||
| text[zArray[index]] === text[index + zArray[index]] | ||
| ) { | ||
| zArray[index]++ | ||
| } | ||
| // If index + zArray[index] - 1 is greater than right, we update values of variables left and right | ||
| if (index + zArray[index] - 1 > right) { | ||
| left = index | ||
| right = index + zArray[index] - 1 | ||
| } | ||
| } | ||
| return zArray | ||
| } | ||
| export default zFunction |
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,8 @@ | ||
| import zFunction from '../ZFunction' | ||
| test('Testing zFunction', () => { | ||
| expect(zFunction('aabxaayaab')).toEqual([10, 1, 0, 0, 2, 1, 0, 3, 1, 0]) | ||
| expect(zFunction('aabxaabxcaabxaabxay')).toEqual([ | ||
| 19, 1, 0, 0, 4, 1, 0, 0, 0, 8, 1, 0, 0, 5, 1, 0, 0, 1, 0 | ||
| ]) | ||
| }) |
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.