|
1 |
| -# [28. Implement strStr()](https://leetcode.com/problems/implement-strstr) |
| 1 | +# [28. Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string) |
2 | 2 |
|
3 |
| -[中文文档](/solution/0000-0099/0028.Implement%20strStr%28%29/README.md) |
| 3 | +[中文文档](/solution/0000-0099/0028.Find%20the%20Index%20of%20the%20First%20Occurrence%20in%20a%20String/README.md) |
4 | 4 |
|
5 | 5 | ## Description
|
6 | 6 |
|
7 |
| -<p>Implement <a href="http://www.cplusplus.com/reference/cstring/strstr/" target="_blank">strStr()</a>.</p> |
8 |
| - |
9 | 7 | <p>Given two strings <code>needle</code> and <code>haystack</code>, return the index of the first occurrence of <code>needle</code> in <code>haystack</code>, or <code>-1</code> if <code>needle</code> is not part of <code>haystack</code>.</p>
|
10 | 8 |
|
11 |
| -<p><strong>Clarification:</strong></p> |
12 |
| - |
13 |
| -<p>What should we return when <code>needle</code> is an empty string? This is a great question to ask during an interview.</p> |
14 |
| - |
15 |
| -<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> |
16 |
| - |
17 | 9 | <p> </p>
|
18 |
| -<p><strong>Example 1:</strong></p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
19 | 11 |
|
20 | 12 | <pre>
|
21 |
| -<strong>Input:</strong> haystack = "hello", needle = "ll" |
22 |
| -<strong>Output:</strong> 2 |
| 13 | +<strong>Input:</strong> haystack = "sadbutsad", needle = "sad" |
| 14 | +<strong>Output:</strong> 0 |
| 15 | +<strong>Explanation:</strong> "sad" occurs at index 0 and 6. |
| 16 | +The first occurrence is at index 0, so we return 0. |
23 | 17 | </pre>
|
24 | 18 |
|
25 |
| -<p><strong>Example 2:</strong></p> |
| 19 | +<p><strong class="example">Example 2:</strong></p> |
26 | 20 |
|
27 | 21 | <pre>
|
28 |
| -<strong>Input:</strong> haystack = "aaaaa", needle = "bba" |
| 22 | +<strong>Input:</strong> haystack = "leetcode", needle = "leeto" |
29 | 23 | <strong>Output:</strong> -1
|
| 24 | +<strong>Explanation:</strong> "leeto" did not occur in "leetcode", so we return -1. |
30 | 25 | </pre>
|
31 | 26 |
|
32 | 27 | <p> </p>
|
|
37 | 32 | <li><code>haystack</code> and <code>needle</code> consist of only lowercase English characters.</li>
|
38 | 33 | </ul>
|
39 | 34 |
|
| 35 | + |
40 | 36 | ## Solutions
|
41 | 37 |
|
42 | 38 | <!-- tabs:start -->
|
|
0 commit comments