mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
29 lines
1.4 KiB
HTML
29 lines
1.4 KiB
HTML
<p>Implement <a href="http://www.cplusplus.com/reference/cstring/strstr/" target="_blank">strStr()</a>.</p>
|
|
|
|
<p>Return the index of the first occurrence of needle in haystack, or <code>-1</code> if <code>needle</code> is not part of <code>haystack</code>.</p>
|
|
|
|
<p><strong>Clarification:</strong></p>
|
|
|
|
<p>What should we return when <code>needle</code> is an empty string? This is a great question to ask during an interview.</p>
|
|
|
|
<p>For the purpose of this problem, we will return 0 when <code>needle</code> is an empty string. This is consistent to C's <a href="http://www.cplusplus.com/reference/cstring/strstr/" target="_blank">strstr()</a> and Java's <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)" target="_blank">indexOf()</a>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
<pre><strong>Input:</strong> haystack = "hello", needle = "ll"
|
|
<strong>Output:</strong> 2
|
|
</pre><p><strong>Example 2:</strong></p>
|
|
<pre><strong>Input:</strong> haystack = "aaaaa", needle = "bba"
|
|
<strong>Output:</strong> -1
|
|
</pre><p><strong>Example 3:</strong></p>
|
|
<pre><strong>Input:</strong> haystack = "", needle = ""
|
|
<strong>Output:</strong> 0
|
|
</pre>
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>0 <= haystack.length, needle.length <= 5 * 10<sup>4</sup></code></li>
|
|
<li><code>haystack</code> and <code>needle</code> consist of only lower-case English characters.</li>
|
|
</ul>
|