28. Implement strStr()(String字符串匹配)

题目:

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Subscribe to see which companies asked this question

字符串匹配算法:

算法一:Brute Force(BF或蛮力搜索) 算法

时间复杂度O(MN)

模式串从左往右与匹配串比较,匹配失败,模式串向右移动一位,直到匹配到为止。效率低下,思想简单,就不贴代码了。

算法二:KMP算法

利用不匹配字符的前面那一段字符的最长前后缀来尽可能地跳过最大的距离,需要计算模式串的跳转表next[]。如果在匹配过程中,在字符j时匹配失败,就移动模式串的next[j]到当前字符,继续匹配。

在计算模式串的next[]跳转表时,引入一个概念f(j),其含义是,对于模式串的第j个字符pattern[j],f(j)是所有满足使pattern[1...k-1] = pattern[j-(k-1)...j - 1](k < j)成立的k的最大值。

下面给出根据f(j)值求next[j]的递推公式:

如果 pattern[j] != pattern[f(j)],next[j] = f(j);

如果 pattern[j] = pattern[f(j)],next[j] = next[f(j)];

代码:

public class Solution {
	public int[] next;
    public int strStr(String haystack, String needle) {
    	if(needle.isEmpty())return 0;
    	if(haystack.isEmpty())return -1;
        next = new int[needle.length()+1];
        getNext(needle);
        int j=0,i=0;
        while(i<haystack.length())
        {
        	while(j>=0 && haystack.charAt(i)!=needle.charAt(j))j=next[j+1]-1;
        	j++;
        	i++;
        	if(j==needle.length())return i-j;
        }
        return -1;
    }
    
    public void getNext(String str)
    {
    	//next从1开始
    	int i=1,f=0;
    	while(i<str.length())
    	{
    		//找到满足pattern[k] = pattern[j]的最大k值。
    		while(f>0 && str.charAt(i-1)!=str.charAt(f-1))
    		{
    			f=next[f];
    		}
    		i++;
    		f++;
    		if(str.charAt(i-1)==str.charAt(f-1))
    		{
    			next[i]=next[f];
    		}
    		else
    		{
    			next[i]=f;
    		}
    	}
    }
}
算法三: BM(Boyer-Moore)算法 


算法四:Sunday算法

如果匹配失败,则查找目标串中,与模式串对齐最后一个字符的后面一个字符ch,并查找模式串中等于ch的字符,使之与目标串的ch对齐。

public class Solution {
	public int strStr(String haystack, String needle) {
    	if(needle.isEmpty())return 0;
    	if(haystack.isEmpty())return -1;
		int[] dic = new int[26];
		for(int i=0;i<26;i++)
		{
			dic[i]=needle.length()+1;
		}
		for(int i=0;i<needle.length();i++)
		{
			//设置每个字符离needle尾字符的长度加一
			dic[needle.charAt(i)-'a']=needle.length()-i;
		}
		//目标字符串与模式串对其位置
		int pos=0;   
		int i,j;
		while(pos<haystack.length()-needle.length()+1)
		{
			i=pos;
			for(j=0;j<needle.length();++j,++i)
			{
				if(haystack.charAt(i)!=needle.charAt(j))
				{
					//查找目标串中,与模式串对齐最后一个字符的后面那个字符ch,
					//并查找模式串中等于ch的字符,使之与目标串的ch对齐
					if(pos+needle.length()<haystack.length()-needle.length()+1)
					pos+=dic[haystack.charAt(pos+needle.length())-'a'];
					else pos++;
					break;
				}
			}
			if(j==needle.length())return pos;
		}
		
		return -1;
	}
}


参考文献:http://blog.csdn.net/joylnwang/article/details/6778316/

http://blog.163.com/yangfan876@126/blog/static/80612456201342205056344/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值