LeetCode 28. Implement strStr()

Description

https://leetcode.com/problems/implement-strstr/

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Explanation

The problem is asking for the index of the first occurrence of needle in haystack.

The idea to solve the problem is that we check whether is able to loop through each character of needle from one position of haystack.

Time Complexity: O((m – n + 1) * n) = O(m * n). m is the length of haystack. n is the length of needle.

Video Tutorial

Java Solution

Leave a Reply

Your email address will not be published. Required fields are marked *