implement Rabin-Karp. Super easy to understand! Beats 74% submissions.


  • 0
    T
    public class Solution {
        public int strStr(String haystack, String needle) {
            
            int len = needle.length();
            
            int code2 = needle.hashCode();
            
            for(int i = 0; i <= haystack.length() - len; i ++){
                
                int code1 = haystack.substring(i, i + len).hashCode();
                if(code1 == code2)
                    return i;
            }
            
            return -1;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.