Monday, February 1, 2016

LeetCode Q28: Implement strStr()

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

This question is suppose to be very easy, however, I confuse this question with LeetCode Q3: Longest substring without repeating characters. Thus made many mistakes in the first several tries.

Basically, the simplest solution is brute force one which is similar to code below:

Yet, the optimal solution is KMP algorithm which I don't think most people can understand and are expected to finish during interview, so skip.



Rount 2.
Use Boyer-Moore algorithm which is easier to understand than KMP and 3-5 times faster than KMP.

No comments:

Post a Comment