strStr

strstr (a.k.a find sub string), is a useful function in string operation. Your task is to implement this function.

For a given source string and a target string, you should output the firstindex(from 0) of target string in source string.

If target does not exist in source, just return -1.

Have you met this question in a real interview?  
Yes
Example

If source = "source" and target = "target", return -1.

If source = "abcdabcdefg" and target = "bcd", return 1.

Challenge

O(n2) is acceptable. Can you implement an O(n) algorithm? (hint: KMP)

Clarification

Do I need to implement KMP Algorithm in a real interview?

  • Not necessary. When you meet this problem in a real interview, the interviewer may just want to test your basic implementation ability. But make sure your confirm with the interviewer first.

题目至少有3种方法,第一种是暴力搜索,每次移动一步,则时间复杂度为O(nm),其中n表示source的长度,m表示target的长度。第二种方法是在第一种方法的基础上做了改进,每次对比时,同时从两边向中间夹,这样可以适当降低时间复杂度;第三种方法,肯定就是KMP了,KMP算法优化的是第一二种方法存在的回溯问题,第一二种方法中,每次移动i到达某个值,使得source[i] != target[j]的时候,会回溯到i - j + 1的位置,也就是说[i-j+1, i]这个区间又要重复计算;

KMP算法的提出就是要避免此回溯,使得i只往后移,也就是说,这样的时间复杂度将是O(N),N为source的长度。KMP的算法证明,就不详细展开了;这里主要讲下next[j]的求解,其实在求解next[j]的过程有递归的含义在其中,也是在做一个字符串的匹配问题,有两个变量k,j,分别初始化为k = -1, j = 0,next[0] = -1;当k == -1 或者 target[k] == target[j]的时候,则说明next[j + 1] = k + 1; 否则的话,则将k移动到next[k],这一步就包括了递推的字符串匹配在其中,理解起来比较难;

class Solution {
public:
    /**
     * Returns a index to the first occurrence of target in source,
     * or -1  if target is not part of source.
     * @param source string to be scanned.
     * @param target string containing the sequence of characters to match.
     */
    //KMP算法
    void GetNext(const char *p, vector<int> &next)
    {
        int len = strlen(p);
        int k = -1, j = 0;
        while(j < len - 1)
        {
            if(k == -1 || p[k] == p[j])
            {
                next[++j] = ++k;
            }
            else
                k = next[k];
        }
    }
    
    
    int strStr(const char *source, const char *target) {
        // write your code here
        if(source == NULL || target == NULL)
            return -1;
        int lens = strlen(source);
        int lent = strlen(target);
        if(lens < lent)
            return -1;
        int i = 0, j = 0;
        vector<int> next(lent, -1);
        GetNext(target, next);
        while(i < lens && j < lent)
        {
            if(j == -1)
            {
                ++i;
                j = 0;
            }
            else if(source[i] == target[j])
            {
                ++i;
                ++j;
            }
            else 
            {
                j = next[j];
            }
        }
        
        if(j >= lent)
            return i - j;
        return -1;
    }
};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值