kmp java

    static int KMPSearch(String pat, String txt) {
        ArrayList<Integer> matchAtIndexs = new ArrayList<>();
        int M = pat.length();
        int N = txt.length();
        int lps[] = new int[M];
        int j = 0;
        computeLPSArray(pat, M, lps);
        int i = 0;
        while (i < N) {
            if (pat.charAt(j) == txt.charAt(i)) {
                j++;
                i++;
            }
            if (j == M) {
                matchAtIndexs.add(i - j);
                j = lps[j - 1];
            } else if (i < N && pat.charAt(j) != txt.charAt(i)) {
                if (j != 0)
                    j = lps[j - 1];
                else
                    i = i + 1;
            }
        }
        return matchAtIndexs.size();
    }

    static void computeLPSArray(String pat, int M, int lps[]) {
        int len = 0;
        int i = 1;
        lps[0] = 0;
        while (i < M) {
            if (pat.charAt(i) == pat.charAt(len)) {
                len++;
                lps[i] = len;
                i++;
            } else {
                if (len != 0) {
                    len = lps[len - 1];

                } else {
                    lps[i] = len;
                    i++;
                }
            }
        }
    }

3.33
3
Phoenix Logan 186120 points

                                        ArrayList&lt;Integer&gt; matachedAtindex;

    void KMPSearch(char[] pat, char[] txt) {
        int m = pat.length;
        int n = txt.length;
        int j = 0;
        int i = 0;
        matachedAtindex = new ArrayList&lt;&gt;();
        lpsCompute(pat);
        while (i &lt; n) {
            if (pat[j] == txt[i]) {
                j++;
                i++;
            }
            if (j == m) {
                matachedAtindex.add((i - j));
                j = lps[j - 1];
            } else if (i &lt; n &amp;&amp; pat[j] != txt[i]) {
                if (j != 0)
                    j = lps[j - 1];
                else
                    i = i + 1;
            }
        }
    }
    
    static int lps[];

    static void lpsCompute(char[] pat) {
        int n = pat.length;
        lps = new int[n];
        int j = 0;
        int i = 1;
        while (i &lt; n) {
            if (pat[i] == pat[j]) {
                j++;
                lps[i] = j;
                i++;
            } else {
                if (j != 0) {
                    j = lps[j - 1];
                } else {
                    lps[i] = j;
                    i++;
                }
            }
        }
    }

3.33 (3 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source