30-seconds-of-java
30-seconds-of-java copied to clipboard
Implement Rabin-Karp algorithm for substring search
Summary
This PR introduces the Rabin-Karp algorithm for substring search in Java. It includes:
RabinKarpSubstringSearchSnippetclass with:rabinKarpSearchmethod to find the first occurrence of a substring.- Efficient hash computation using a large prime.
- Sliding window hash recalculation.
- Substring equality check for collision handling.
RabinKarpSubstringSearchSnippetTestclass similar to the tests in KMP for testing and comparisons.- A ready-to-use
README.mdsnippet documenting usage. - BUILD SUCCESSFUL
Benefits
- Provides an alternative to KMP for substring search.
- Suitable for multiple pattern searches with rolling hash.
- Modular, clean, and well-documented code for educational and production use.
Notes
- Uses
1_000_000_007as prime to reduce hash collisions. - Designed for clarity and maintainability.