Problem 28: Find the Index of the First Occurrence in a String
Question
LeetCode Link | 28. Find the Index of the First Occurrence in a String | Easy
Given two strings needle
and haystack
, return the index of the first occurrence of needle
in haystack
, or -1
if needle
is not part of haystack
.
Constrains
1 <= haystack.length, needle.length <= 10^4
haystack
andneedle
consist of only lowercase English characters.
Examples
Example 1
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.
Example 2
Input: haystack = “leetcode”, needle = “leeto”
Output: -1
Explanation: “leeto” did not occur in “leetcode”, so we return -1.
Solution Approach
Method: KMP
Constrains
- None
This method does not have any limitations.
Concept Explanation
This question can be solve by brute force, which is basically compare each index of pattern with string, and re-compare form the start if failed.
1 | function strStr(haystack: string, needle: string): number { |
This cost O(n * m) time since we match from start of needle
each time, which could waste lots of time if some pattern in needle
is overlaped.
For example, we have haystack aaaaaaaaf
and needle aaaaf
, it wil always re-match from first character a
, but we could save this time since first four character are the same.
Introducing the KMP Algorithm, this is a string matching algorithm designed for searching substrings or patterns within a main text string efficiently. By using KMP, we could only compare pattern after last a
in needle, which is much more efficiently. For more detail, check the post The KMP Algorithm.
To use KMP, we have to compute the LPS array first. This array contains the index we need to go back to if we failed to match the string. For more detail on how to create a LPS array, check the post The KMP Algorithm.
Then we can iterate haystack
string to find if needle
match by comparing character just like brute force. However, if we failed to match character, we can just use LPS array to go to the nearest index that we already validate all the characters before this index.
Code
- Time complexity: O(n + m)
- Space complexity: O(m), need
m
space to storeLPS
TypeScript
1 | function strStr(haystack: string, needle: string): number { |
Conclusion
Using the LPS array, the KMP algorithm significantly reduces the time complexity of the search phase, making it efficient for searching substrings in larger texts. Try to understand the logic of the KMP algorithm instead of memories the code, this is a commonly used algorithm in string comparisons, memories the hard code would not help you during interview:(