28. Find the Index of the First Occurrence in a string

82 篇文章 0 订阅
49 篇文章 0 订阅

The Description of the problem

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = “sadbutsad”, needle = “sad”
Output: 0
Explanation: “sad” occurs at index 0 and 6. The first occurrence is at index 0, so we return 0.

Example 2:

Input: haystack = “leetcode”, needle = “leeto”
Output: -1
Explanation: “leeto” did not occur in “leetcode”, so we return -1.

The codes in Python

class Solution:
	def strStr(self, haystack: str, needle: str) -> int:
		if len(needle) == 0:
			return 0
		# get next array
		next = [0]
		pos = 0
		for i in range(1, len(needle)):
			while pos != 0 and needle[pos] != needle[i]:
				pos = next[pos - 1]
			if needle[pos] == needle[i]:
				pos += 1
			next.append(pos)
		# find the index of the first occurrence of needle in haystack
		j = 0
		for i in range(len(haystack)):
			while j >= 1 and haystack[i] != needle[j]:
				j = next[j - 1]
			if haystack[i[ == needle[j]:
				j += 1
			if j == len(needle):
				return i - j + 1
		return -1

The codes in C++

The codes with a bug (Can you find it and fix it)

class Solution {
public:
    int strStr(string haystack, string needle) {
        if (needle.size() == 0) {
            return 0;
        }
        // get the next array
        vector<int> next = {0};
        int pos = 0;
        for (int i = 1; i < needle.size(); i++) {
            while (pos > 0 && needle[pos] != needle[i]) {
                pos = needle[pos - 1];
            }
            if (needle[pos] == needle[i]) {
                pos++;
            }
            next.push_back(pos);
        }
        // find the index of the first occurrence of needle in haystack
        int j = 0;
        for (int i = 0; i < haystack.size(); i++) {
            while (j > 0 && haystack[i] != needle[j]) {
                j = next[j - 1];
            }
            if (haystack[i] == needle[j]) {
                j++;
            }
            if (j == needle.size()) {
                return i - needle.size() + 1;
            }
        }
        return -1;
    }
};

The C++ codes with bug free

class Solution {
public:
    int strStr(string haystack, string needle) {
        if (needle.size() == 0) {
            return 0;
        }
        // get the next array
        vector<int> next = {0};
        int pos = 0;
        for (int i = 1; i < needle.size(); i++) {
            while (pos > 0 && needle[pos] != needle[i]) {
                pos = next[pos - 1];
            }
            if (needle[pos] == needle[i]) {
                pos++;
            }
            next.push_back(pos);
        }
        // find the index of the first occurrence of needle in haystack
        int j = 0;
        for (int i = 0; i < haystack.size(); i++) {
            while (j > 0 && haystack[i] != needle[j]) {
                j = next[j - 1];
            }
            if (haystack[i] == needle[j]) {
                j++;
            }
            if (j == needle.size()) {
                return i - needle.size() + 1;
            }
        }
        return -1;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值