Input: txt = "THIS IS A TEST TEXT" pat = "TEST" Output: Pattern found at index 10 Input: txt = "AABAACAADAABAABA" pat = "AABA" Output: Pattern found at index 0 Pattern found at index 9 Pattern found at index 12
The Naive String Matching algorithm slides the pattern one by one. After each slide, it one by one checks characters at the current shift and if all characters match then prints the match.
Like the Naive Algorithm, Rabin-Karp algorithm also slides the pattern one by one. But unlike the Naive algorithm, Rabin Karp algorithm matches the hash value of the pattern with the hash value of current substring of text, and if the hash values match then only it starts matching individual characters. So Rabin Karp algorithm needs to calculate hash values for following strings.
1) Pattern itself.
2) All the substrings of text of length m.
Pattern found at index 0 Pattern found at index 10
Please refer complete article on Rabin-Karp Algorithm for Pattern Searching for more details!
- LongStream.Builder build() in Java
- DoubleStream.Builder build() in Java
- Java 8 | Consumer Interface in Java with Examples
- MouseListener and MouseMotionListener in Java
- Java Program for Selection Sort
- Java Program for Bubble Sort
- Java Program for Insertion Sort
- Java Program for Heap Sort
- Java Program for Radix Sort
- Java Program for n-th Fibonacci numbers
- Java Program for Counting Sort
- Java Program for ShellSort
- Java Program for Longest Common Subsequence
- Java Program for Binary Search (Recursive and Iterative)
- Java Program 0-1 Knapsack Problem
- Java Program for Min Cost Path
- Java Program for Longest Increasing Subsequence
- Java Program for GCD of more than two (or array) numbers
- Java Program for Common Divisors of Two Numbers
- Java Program for Bitonic Sort