My Java Boyer-Moore solution


  • 0
    R
    public class Solution {
    
    public int strStr(String haystack, String needle) {
        char[] text = haystack.toCharArray();
    	char[] pattern = needle.toCharArray();
    	if(text.length<pattern.length) return -1;
    	int tstartind = 0,pind = pattern.length-1,tind=tstartind+pind;
    	while(tind<text.length){
    		while(pind>=0&&text[tind] == pattern[pind])
    		{
    			tind--;pind--;
    		}
    		if(pind<0) return tstartind;
    		while(pind>=0&&text[tind]!=pattern[pind])
    		{
    			pind--;
    		}
    		tstartind = tind-pind;
    		pind = pattern.length-1;
    		tind = tstartind+pind;
    	}
    	return -1;
    }
    

    }


Log in to reply
 

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