Skip to content

Adding KMP String Matching Algorithm #18

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion bin/.gitignore

This file was deleted.

Binary file modified bin/chapter03linkedlists/DoublyLinkedList.class
Binary file not shown.
Binary file modified bin/chapter03linkedlists/MergeKSortedLists.class
Binary file not shown.
Binary file modified bin/chapter04stacks/DynamicArrayStack.class
Binary file not shown.
Binary file modified bin/chapter06trees/FindLevelwithMaxSum.class
Binary file not shown.
Binary file modified bin/chapter07priorityqueues/MergingKSortedLists$1.class
Binary file not shown.
80 changes: 80 additions & 0 deletions src/chapter15stringalgorithms/KMPStringMatchingAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package chapter15stringalgorithms;

import java.io.*;

public class KMPStringMatchingAlgorithm {

public static void main(String[] args) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int tc = Integer.parseInt(br.readLine());
while(tc-->0) {
String[] input = br.readLine().split(" ");
char[] pattern = input[0].toCharArray();
char[] text = input[1].toCharArray();
if(pattern.length==1){
bw.write(String.valueOf(getNoOfMatchedString(text,pattern)));
} else {
bw.write(String.valueOf(getNoOfMatchedStrings(text,pattern)));
}

bw.newLine();
}
bw.close();
}

private static int getNoOfMatchedString(char[] text, char[] pattern) {
char p = pattern[0];
int count = 0;
for(int idx=0;idx<text.length;idx++){
if(text[idx]==p){
count++;
}
}
return count;
}

public static int getNoOfMatchedStrings(char[] text, char[] pattern){
int p1=0;
int p2=0;
int count = 0;
int[] lps = getLPS(pattern);
while(p1<text.length){
if(text[p1]==pattern[p2]){
if(p2==pattern.length-1){
count++;
p2=lps[p2-1];
} else {
p1++;
p2++;
}
} else if(p2>0){
p2 = lps[p2-1];
} else {
p1++;
}
}
return count;
}

public static int[] getLPS(char[] pattern){
int[] lps = new int[pattern.length];
int p1 = 0;
int p2 = 1;
lps[0] = 0;
while(p2<pattern.length){
if(pattern[p2]==pattern[p1]){
lps[p2]=p1+1;
p1++;
p2++;
} else if(p1>0){
p1 = lps[p1-1];
} else {
lps[p2]=0;
p2++;
}
}
return lps;
}
}