Implement strStr()

Question

Implement strStr().

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

Solution

First Solution

C++ (brute force):

class Solution {
public:
    int strStr(string haystack, string needle) {
        for(int i=0;;i++){
            for(int j=0;;j++){
                if(j==needle.length()) return i;
                if(i==haystack.length()) return -1;
                if(needle[j] != haystack[i+j]) break;
            }
        }
    }
};

result: time limit exceeded, so I make some changes to optimize the algorithm. I reduce operations in each iteration.

class Solution {
public:
    int strStr(string haystack, string needle) {
        int i = 0, j = 0,lenn = needle.length(),lenh = haystack.length();
        if (lenn == 0)  return 0;
        for(;i<=lenh-lenn;i++){
            for(j=0;;j++){
                if(j==lenn) return i;
                if(needle[j] != haystack[i+j]) break;
            }
        }
        return -1;
    }
};

Second Solution

My Solution:

class Solution {
public:
    static vector<int> KMP;
    static int strStr(string haystack, string needle);
    static void calKMP(string needle);
};

vector<int> Solution::KMP;
int Solution::strStr(string haystack, string needle) {
    calKMP(needle);
    int i = 0, j = 0, lenn = needle.length(), lenh = haystack.length();
    for (i = 0;i<=lenh-lenn;) {
        for (j = 0;; j++) {
            if (j == lenn) return i;
            if (needle[j] != haystack[i + j]) {
                if (j == 0) i++;
                else i = i + (j - KMP[j-1]);             //根据Partial Match Table来确定i后移的位数
                break;
            }
        }
    }
    return -1;
}

void Solution::calKMP(string needle) {
    unordered_map<string, int> hash;              //用于前缀和后缀匹配
    int i = 0,j = 0,k = 0,max_num = 0,lenn = needle.length(); 
    for (; j < lenn;j++){
        max_num = 0;
        for (; k <= j; k++) {
            hash.insert({ needle.substr(0,k+1),k+1 });             //存入前缀
        }
        for (i=j; i > 0; i--) {
            unordered_map<string, int>::iterator it = hash.find(needle.substr(i, j-i+1));        //后缀与前缀匹配
            if (it != hash.end()) {
                max_num = max(it->second, max_num);                //取最大字串的字符数
            }        
            else continue;
        }
        KMP.push_back(max_num);                          //存入Partial Match Table
    }
}

I also got a time limit exceeded using this method, I think it has to do with the calKMP part but I haven't fixed it up yet. I'll work on that someday...

原文地址:我的主页文章

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 标签: C++ 算法 LeetCode 字符串 KMP 每日算法——leetcode系列 问题 Implemen...
    CarlBlack阅读 2,444评论 0 0
  • 东西不要第二天再去吃 不是我要强 只是此生错过一个你就够了 想说的话在对话框里删了又删 点开你...
    装甲车老司机阅读 4,322评论 0 0
  • 这两天牙疼,照镜子发现有个洞,一吃饭就受影响,所以不得不去医院了,上午跑了两家医院都没挂上号,先是海淀医院,不能预...
    糖宝阅读 3,196评论 0 1
  • 最近几天心情特别低沉,总是会时不时的回忆起以前的事情,窥探自己的性格,以至于现在自己都越发的讨厌自己了。最近朋友A...
    湉淼淼的restroom阅读 1,196评论 0 0
  • 那天下午,孩子的半期考成绩就出来了。 我知道孩子这次肯定会进步,但没想到孩子进步会这么大,前进了70多名,真的让我...
    西瓜甜甜啦阅读 3,493评论 11 20