Implement strStr()
Description
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll" Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba" Output: -1
Clarification:
What should we return when needle
is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle
is an empty string. This is consistent to C's strstr() and Java's indexOf().
Constraints:
haystack
andneedle
consist only of lowercase English characters.
Solution(javascript)
/** Rabin-Karp
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
const strStr = function (haystack, needle) {
const base = 256
const prime = 1439173969
if (needle === '') {
return 0
}
let hashNeedle = 0
const magic = (base ** (needle.length - 1)) % prime
const product = prime * base
for (const c of needle) {
hashNeedle = (hashNeedle * base + c.charCodeAt(0)) % prime
}
const isEqual = i => needle === haystack.slice(i, i + needle.length)
let hash = 0
for (let i = 0; i <= haystack.length - needle.length; i++) {
if (i === 0) {
for (let j = 0; j < needle.length; j++) {
hash = (hash * base + haystack[j].charCodeAt(0)) % prime
}
} else {
hash = (hash - haystack[i - 1].charCodeAt(0) * magic + product) % prime
hash = (hash * base + haystack[i + needle.length - 1].charCodeAt(0)) % prime
}
if (hash === hashNeedle && isEqual(i)) {
return i
}
}
return -1
}