Leetcode 28 || Find the Index of the First Occurrence in a String
Leetcode Daily Challenge March 03,2023
Table of contents
In this blog, I will explain the mentioned leetcode problem and the solution.
Problem :
Link : https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/
The problem description says it has two input strings a haystack
and a needle
. We have to find out if any substring matches the needle string. And the output will be the index of the substring in the haystack string. If there are multiple substrings we need to return the index of the first substring. If no substring matches we need to return -1 as output.
For example,
example 01: input : haystack = 'stringstr' , needle = 'str' output : 0
Here, in haystack
at index 0 to 2, there is a match, and another match is found at index 6 to 8. So the output will be 0 as it is the first substring.
example 02 : input : haystack = 'matter' , needle = 'matc' output : -1
Here, there is no substring equal to needle
in haystack
so the output is -1
Solution :
Let's solve the problem now.
First of all, we need to check if the first index of the needle matches any index of the haystack.
Then if we find the first matched index at
haystack
we need to check if the whole needle matches or notIf only the first index matches and there are mismatches later, we need to iterate the next index of
haystack
where the first index ofneedle
matches.If the loop ends without any matches return -1
If any substring matches we do not need to iterate further on
haystack
string.
One thing is important to discuss here. Do we need to iterate through a loop that ends at the last index of
haystack
? What do you think ?Let's look at an example,
input : haystack = 'abcdefghijk' , needle = 'matter' output : -1
To solve this problem we will iterate through
haystack
in search of theneedle[0]
means'm'
. If we iterate onhaystack
til the last index we will end at'k'
. But do we really need this?The length of
needle
andhaystack
is 6 and 11 here respectively.So if (11-6)= 5th index of haystack does not match needle[0], searching in the rests is meaningless. So we should iterate through
0 to (length of
haystack
- length ofneedle
)th index.Code :
Let's jump into code.
class Solution: def strStr(self, haystack: str, needle: str) -> int: # default start_index = -1 start_index = -1 len_h , len_n = len(haystack), len(needle) i = 0 # we will run the loop till len_h-len_n, not till the end while i<len_h-len_n+1: if haystack[i]==needle[0]: # here we found the first match, so we will change the start_index to i start_index = i j = 0 # its time to check if the whole needle matches or not while j<len_n: if haystack[j+start_index]!=needle[j]: # if there is any mismatch , change start_index to -1 and break the loop start_index = -1 break j += 1 # j == len_n means the needle matches that started at index start_index, so return the start_index if j==len_n: return start_index i += 1 return start_index
note: In the code above I've tried to solve in a way so you can understand how to think and solve step by step without using any shortcut tricks or built-in functionality.
Thank you so much for taking the time to read this far! Your thoughts and feedback mean a lot to me, so please don't hesitate to share them with me. Whether you enjoyed it or not, any comments and suggestions you have are most welcome and appreciated