Implement strStr()

You are given two strings, haystack and needle.

Find the first position where needle occurs as a substring of haystack.

Return the 0-based index of the first character of this occurrence. If needle does not occur anywhere in haystack, return -1.

The matching must be exact and is case-sensitive.

Example 1:

Input: haystack = "programming", needle = "gram"

Output: 3

Explanation: The substring "gram" first appears starting at index 3.

Example 2:

Input: haystack = "mississippi", needle = "issip"

Output: 4

Explanation: The substring "issip" first appears starting at index 4 in "mississippi".

Now Your Turn!

Pick the correct output for the given input

Input: haystack = "algorithm", needle = "rhythm"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

1 <= haystack.length <= 10^4

1 <= needle.length <= 10^4

haystack and needle consist only of lowercase English letters.

Fun Facts

0
class Solution {
public:
int strStr(string haystack, string needle) {
// Your code goes here
return -1;
}
};
Test Case

Input:

Haystack
Needle