[LeetCode] 028. Implement strStr() (Easy) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode


028. Implement strStr() (Easy)

链接

题目:https://oj.leetcode.com/problems/implement-strstr/
代码(github):https://github.com/illuz/leetcode

题意

在一个字符串里找另一个字符串在其中的位置。

分析

这题归在 Easy 类是因为它 O(n*n) 的暴力能过。
如果数据强点就得 Midium 以上了。

  1. (C++) 这题的暴力 O(n^2) 就是两遍 for 去找
  2. (C++) 还有各种高大上的算法,比如 KMP 算法,这是经典的了,具体实现见 我的另一篇博客
  3. (Python) 另外可以用 hash 去做,叫 rolling hash 算法(见 Wiki StackOverflow),就是把字符串 hash 出来,按匹配串长度窗口去滚动,再去匹配。hash 字符串有很多种方法,这边的字母好像都是小写,有 26 个,所以就用 29 做基数(本来想像 djb2 算法用 33 做基数,可以直接 ((hash << 5) + hash) 很快,不过 int 范围只能 hash 6 个字母而且 rolling 的时候还是要 /33,还是用 29 算了),超 int 范围的话用 Python 就不用考虑这个问题了。

其他还有 Boyer–Moore,Rabin–Karp 算法,具体自己去搜。

代码

C++: 暴力

class Solution {
public:
    int strStr(char *haystack, char *needle) {
		int hlen = strlen(haystack);
		int nlen = strlen(needle);
		if (hlen < nlen)
			return -1;
		for (int i = 0; i <= hlen - nlen; i++) {
			if (!strncmp(haystack + i, needle, nlen))
				return i;
		}
		return -1;
    }
};


C++: KMP

class Solution {
private:
	int next[1000100];
public:
    int strStr(char *haystack, char *needle) {
		int hlen = strlen(haystack);
		int nlen = strlen(needle);
		int i, j;
		if (hlen < nlen)
			return -1;
		if (nlen == 0)
			return 0;

		// KMP
		// get next matching array
		// int *next = new int(nlen + 1);
		// Because the string will be 1 million, so we cannot use `int *next = new int(nlen + 1)`, it will exceed the memory in function
		i = 0;
		j = -1;
		next[0] = -1;
		while (i < nlen) {
			if (j == -1 || needle[i] == needle[j]) {
				i++;
				j++;
				next[i] = j;
			} else
				j = next[j];
		}

		// matching
		i = 0;
		j = 0;
		while (i < hlen) {
			if (j == -1 || haystack[i] == needle[j]) {
				i++;
				j++;
			} else
				j = next[j];
			if (j == nlen)
				return i - nlen;
		}
		
		return -1;
    }
};


Python:

class Solution:
    # @param haystack, a string
    # @param needle, a string
    # @return an integer
    def strStr(self, haystack, needle):
        hlen, nlen = len(haystack), len(needle)
        if nlen == 0:
            return 0
        if nlen > hlen or hlen == 0:
            return -1

        rolling = lambda x, y: x * 29 + y
        get_hash = lambda ch: ord(ch) - ord('a')

        nhash = reduce(rolling, map(get_hash, needle))
        hhash = reduce(rolling, map(get_hash, haystack[:nlen]))
        if nhash == hhash:
            return 0

        high_base = 29 ** (nlen - 1)
        for i in range(nlen, hlen):
            hhash -= get_hash(haystack[i - nlen]) * high_base # remove first in hash code
            hhash = rolling(hhash, get_hash(haystack[i]))     # add new
            if nhash == hhash:
                return i - nlen + 1

        return -1


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值