-
Notifications
You must be signed in to change notification settings - Fork 19.9k
Z Algorithm #5959
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
Z Algorithm #5959
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks good, please check why PR checks are failing
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #5959 +/- ##
============================================
+ Coverage 67.43% 69.10% +1.67%
- Complexity 4578 4673 +95
============================================
Files 620 624 +4
Lines 17147 17176 +29
Branches 3312 3315 +3
============================================
+ Hits 11563 11870 +307
+ Misses 5138 4854 -284
- Partials 446 452 +6 ☔ View full report in Codecov by Sentry. |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution! |
Please reopen this pull request once you have made the required changes. If you need help, feel free to ask in our Discord server or ping one of the maintainers here. Thank you for your contribution! |
The Z Algorithm is an efficient string matching algorithm that computes a Z-array for a given string, which indicates the length of the longest substring starting from each index that matches the prefix of the string.
Key Points:
Z-array: For a string S, Z[i] represents the length of the longest substring starting at index i that is also a prefix of S.
Time Complexity: The algorithm runs in linear time, O(n + m), where n is the length of the text and m is the length of the pattern, making it faster than naive approaches.
Pattern Matching: By combining the pattern and the text into a single string (e.g., pattern$text), the Z-array helps efficiently locate occurrences of the pattern within the text.
Applications: It is widely used in text processing, DNA sequence analysis, and competitive programming for substring searching.
The Z Algorithm is valued for its efficiency and simplicity, allowing for quick implementation in various string matching scenarios.