Java implementation of KMP algorithm

Source: Internet
Author: User

KMP algorithm Explanation

My Java Code implementation

Package Jj4_6;public class KMP {public static Boolean contains (string a,string b) {/* * First step, do a preprocessing on B string */integer[] Fuzhu = New Integer[b.length ()];for (Integer i= 0;i<b.length (); i++) {//I = = 3 o'clock//Nowstr = "Abab" String nowstr =b.substring (0, I+1); Fuzhu[i] = 0;for (Integer j =i;j>0;j--) {//left = ' aba ' String left = nowstr.substring (0,J);//right = "Bab"; String right = nowstr.substring (i-j+1, Nowstr.length ()), if (Left.equals (right)) {fuzhu[i] = J;break;}}}  /* * Complete matching */for (integer i = 0;i <= a.length ()-b.length ();) {integer j;for (j =0;j<b.length (); j + +) {if (A.charat (i+j)! = B.charat (j)) {/* * calculates the moving distance of I */if (j==0) I++;elsei = i +  ((j-1) + 1-fuzhu[(j-1)]);/* * Restart match */break;}} if (j = = B.length ()) return true; return false;}}

Java implementation of KMP algorithm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.