iluwatar / 30-seconds-of-java

Collection of reusable tested Java 17 compatible code snippets that you can understand in 30 seconds or less.
https://java-design-patterns.com/snippets.html
MIT License
1.04k stars 409 forks source link

Add KMP Substring Search Algorithm #231

Closed pandeysubash404 closed 1 week ago

pandeysubash404 commented 1 month ago

Adding more code file required for #219

Implements the Knuth-Morris-Pratt (KMP) algorithm to efficiently find the first occurrence of a pattern in a given text. Uses the Longest Prefix Suffix (LPS) array to minimize unnecessary comparisons, resulting in a time complexity of O(n + m).

kmpSearch("abxabcabcaby", "abcaby") returns 6 since the pattern "abcaby" starts at index 6.

pandeysubash404 commented 1 month ago

@iluwatar

I encountered an error during the 'build-and-analyze' process related to the test case RandomNumberTest > testGetRandomNumber():

RandomNumberTest > testGetRandomNumber() FAILED
    org.opentest4j.AssertionFailedError at RandomNumberTest.java:60

Since this issue seems unrelated to my changes, could you please advise on the next steps? I believe the failure may be due to an existing issue in the test or an external factor.

Thanks for your assistance!

sonarcloud[bot] commented 1 week ago

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarCloud

iluwatar commented 1 week ago

@iluwatar

I encountered an error during the 'build-and-analyze' process related to the test case RandomNumberTest > testGetRandomNumber():

RandomNumberTest > testGetRandomNumber() FAILED
    org.opentest4j.AssertionFailedError at RandomNumberTest.java:60

Since this issue seems unrelated to my changes, could you please advise on the next steps? I believe the failure may be due to an existing issue in the test or an external factor.

Thanks for your assistance!

Looks like flaky RandomNumberTest.java so no worries on your side. I re-ran the build and everything passed.

iluwatar commented 1 week ago

Looks good! Thank you for the contribution 🎉