diff --git a/bin/.gitignore b/bin/.gitignore deleted file mode 100644 index c5e7f18..0000000 --- a/bin/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/chapter21miscconcepts/ diff --git a/bin/chapter03linkedlists/DoublyLinkedList.class b/bin/chapter03linkedlists/DoublyLinkedList.class index 52320c1..0077228 100644 Binary files a/bin/chapter03linkedlists/DoublyLinkedList.class and b/bin/chapter03linkedlists/DoublyLinkedList.class differ diff --git a/bin/chapter03linkedlists/MergeKSortedLists.class b/bin/chapter03linkedlists/MergeKSortedLists.class index 907a4b1..a703384 100644 Binary files a/bin/chapter03linkedlists/MergeKSortedLists.class and b/bin/chapter03linkedlists/MergeKSortedLists.class differ diff --git a/bin/chapter04stacks/DynamicArrayStack.class b/bin/chapter04stacks/DynamicArrayStack.class index a0929fb..0394af2 100644 Binary files a/bin/chapter04stacks/DynamicArrayStack.class and b/bin/chapter04stacks/DynamicArrayStack.class differ diff --git a/bin/chapter06trees/FindLevelwithMaxSum.class b/bin/chapter06trees/FindLevelwithMaxSum.class index 7375818..dafa2c5 100644 Binary files a/bin/chapter06trees/FindLevelwithMaxSum.class and b/bin/chapter06trees/FindLevelwithMaxSum.class differ diff --git a/bin/chapter07priorityqueues/MergingKSortedLists$1.class b/bin/chapter07priorityqueues/MergingKSortedLists$1.class index 25c326e..f42a6d4 100644 Binary files a/bin/chapter07priorityqueues/MergingKSortedLists$1.class and b/bin/chapter07priorityqueues/MergingKSortedLists$1.class differ diff --git a/src/chapter15stringalgorithms/KMPStringMatchingAlgorithm.java b/src/chapter15stringalgorithms/KMPStringMatchingAlgorithm.java new file mode 100644 index 0000000..b9c22f2 --- /dev/null +++ b/src/chapter15stringalgorithms/KMPStringMatchingAlgorithm.java @@ -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;idx0){ + 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(p20){ + p1 = lps[p1-1]; + } else { + lps[p2]=0; + p2++; + } + } + return lps; + } +} \ No newline at end of file