java.lang.Object
g0001_0100.s0028_find_the_index_of_the_first_occurrence_in_a_string.Solution

public class Solution extends Object
28 - Implement strStr().<p>Easy</p> <p>Implement <a href="http://www.cplusplus.com/reference/cstring/strstr/" target="_top">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&rsquo;s <a href="http://www.cplusplus.com/reference/cstring/strstr/" target="_top">strstr()</a> and Java&rsquo;s <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)" target="_top">indexOf()</a>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> haystack = &ldquo;hello&rdquo;, needle = &ldquo;ll&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> haystack = &ldquo;aaaaa&rdquo;, needle = &ldquo;bba&rdquo;</p> <p><strong>Output:</strong> -1</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> haystack = &quot;&quot;, needle = &quot;&quot;</p> <p><strong>Output:</strong> 0</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>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • strStr

      public int strStr(String haystack, String needle)