28. Find the Index of the First Occurence in a String - Easy

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if the needle is not part of haystack.

Example:
Input: haystack = 'sadbutsad', needle = 'sad'

Output: 0

Explanation: 'sad' occurs at index 0 and 6. The first occurrence is at index 0, so we return 0.

Constraints:

1 <= haystack.length, needle.length <= 10**4

haystack and needle consist of only lowercase English characters.

Solution:
var strStr = function(haystack, needle) {

        return haystack.indexOf(needle);

}

Thoughts:
10/10 easy.

Comments

Popular posts from this blog

121. Best Time to Buy and Sell Stock - Easy

58. Length of Last Word - Easy