查找第一次出现的字符串


Submit solution

Points: 10
Time limit: 1.0s
Memory limit: 4M

Author:
Problem type
Allowed languages
C++

查找第一次出现的字符串

给定两个字符串 needle 和 haystack,返回 needle 在 haystack 中首次出现的索引,如果 needle 不在 haystack 中出现,则返回 -1。

示例 1:

输入:

sadbutsad
sad
输出:
0
解释:"sad" 出现在索引 0 和 6 处。首次出现的位置是索引 0,因此返回 0。
示例 2:

输入:

leetcode
leeto

输出:

-1
解释:

"leeto" 没有出现在 "leetcode" 中,因此返回 -1。

约束条件:

\(1 \le haystack.length, needle.length \le 10^4\) haystack 和 needle 仅包含小写的英文字母。


Comments

There are no comments at the moment.