public int patternSearch(char[] text, char[] pattern){
        int m = pattern.length;
        int n = text.length;
        long patternHash = createHash(pattern, m - 1);
        long textHash = createHash(text, m - 1);
        for (int i = 1; i <= n - m + 1; i++) {
            if(patternHash == textHash && checkEqual(text, i - 1, i + m - 2, pattern, 0, m - 1)) {
                return i - 1;
            }
            if(i < n - m + 1) {
                textHash = recalculateHash(text, i - 1, i + m - 1, textHash, m);
            }
        }
        return -1;
    }
Date 09/25/2014
@author Tushar Roy

Rabin Karp algorith for substring matching.

Time complexity in worst case O(n^2)(depends on hash function)
Space complexity O(1)

References
https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm
src/com/interview/string/RabinKarpSearch.java