Skip to content
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

Add Knuth-Morris-Pratt algorithm #1244

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
repositories {
mavenLocal()
maven {
url = 'http://repo.maven.apache.org/maven2'
url = 'https://repo.maven.apache.org/maven2'
}
}

Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/others/KMP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.others;
import java.util.ArrayList;

/**
* Implementation of the Knuth–Morris–Pratt algorithm
*/
public class KMP {

/**
* KMPmatcher method finds if any string "needle" is in the string "haystack" and returns the index of its first letter for all occurrencies.
* @param haystack string in which the method is searching
* @param needle string which the method is searching for
* @return an ArrayList of starting indexes of the haystack that matches the searched needle
*/
public static ArrayList<Integer> KMPmatcher(final String haystack, final String needle) {
final int haystackLength = haystack.length();
final int needleLength = needle.length();
final int[] pi = computePrefixFunction(needle);
int matchingLength = 0;
ArrayList<Integer> startingIndexes = new ArrayList<>();
for (int i = 0; i < haystackLength; i++) {
while (matchingLength > 0 && haystack.charAt(i) != needle.charAt(matchingLength)) {
matchingLength = pi[matchingLength - 1];
}

if (haystack.charAt(i) == needle.charAt(matchingLength)) {
matchingLength++;
}

if (matchingLength == needleLength) {
startingIndexes.add(i + 1 - needleLength);
matchingLength = pi[matchingLength - 1];
}
}
return startingIndexes;
}

/**
* The computePrefixFunction method gets the prefix function of the given string.
* @param P string (this should be the needle in the KNP)
* @return an array of indexes of the given sting where it matches itself
*/
private static int[] computePrefixFunction(final String P) {
final int stringLength = P.length();
final int[] pi = new int[stringLength];
pi[0] = 0;
int matchingLength = 0;
for (int i = 1; i < stringLength; i++) {
while (matchingLength > 0 && P.charAt(i) != P.charAt(matchingLength)) {
matchingLength = pi[matchingLength - 1];
}

if (P.charAt(i) == P.charAt(matchingLength)) {
matchingLength++;
}

pi[i] = matchingLength;

}
return pi;
}
}
22 changes: 22 additions & 0 deletions src/test/java/com/others/KMPTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.others;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

class KMPTest {

@Test
void testKMP() {
KMP kmp = new KMP();

ArrayList<Integer> result = new ArrayList<>();
result.add(0);
result.add(1);
Assertions.assertEquals(result, kmp.KMPmatcher("AAAAABAAABA", "AAAA"), "Incorrect Conversion");

ArrayList<Integer> result2 = new ArrayList<>();
Assertions.assertEquals(result2, kmp.KMPmatcher("ABABABA", "AAAA"), "Incorrect Conversion");
}
}