Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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++;
                }
            }
        }
    }
Comment

kmp java

    ArrayList<Integer> matachedAtindex;

    void KMPSearch(char[] pat, char[] txt) {
        int m = pat.length;
        int n = txt.length;
        int j = 0;
        int i = 0;
        matachedAtindex = new ArrayList<>();
        lpsCompute(pat);
        while (i < n) {
            if (pat[j] == txt[i]) {
                j++;
                i++;
            }
            if (j == m) {
                matachedAtindex.add((i - j));
                j = lps[j - 1];
            } else if (i < n && 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 < n) {
            if (pat[i] == pat[j]) {
                j++;
                lps[i] = j;
                i++;
            } else {
                if (j != 0) {
                    j = lps[j - 1];
                } else {
                    lps[i] = j;
                    i++;
                }
            }
        }
    }
Comment

PREVIOUS NEXT
Code Example
Java :: How to swap two values in Java using a supporting method? 
Java :: Java, how to compare Strings with String Arrays 
Java :: in java how to throw exception from function 
Java :: java random array 
Java :: kjk;ok 
Java :: android dismiss keyboard 
Java :: array contains java 
Java :: java read directory 
Java :: How to find the second smallest value in an array without sorting it in Java? 
Java :: add view to relativelayout programmatically 
Java :: play mp3 android java 
Java :: find min in array java 
Java :: eclipse java content assist 
Java :: convert arraylist to csv file java 
Java :: split method in java 
Java :: Java Create LinkedList in Java 
Java :: java int 
Java :: java remove character from string after 
Java :: How to connect from Android emulator to application on localhost? 
Java :: Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: Trying to create a platform view of unregistered type: plugins.flutter.io/webview 
Java :: get raondom from array java 
Java :: build.plugins.plugin.version 
Java :: spring boot base url 
Java :: java arraylist declaration 
Java :: java print statement 
Java :: java gitignore 
Java :: java initialize string array 
Java :: android studio download 
Java :: java string length validation regex 
Java :: how to find the prime numbers in java 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =