-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement_strStr().py
More file actions
25 lines (22 loc) · 921 Bytes
/
implement_strStr().py
File metadata and controls
25 lines (22 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Problem: Implement strStr()
# (https://leetcode.com/problems/implement-strstr/#/description)
# An implementation of Rabin-Karp
class Solution(object):
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
if len(needle) == 0: return 0
needleHash, candidateNeedleHash = 0, 0
for c in needle:
needleHash += self.hashFunction(c)
for i in range(len(haystack)):
candidateNeedleHash += self.hashFunction(haystack[i])
if i > len(needle) - 1: candidateNeedleHash -= self.hashFunction(haystack[(i - len(needle))])
if candidateNeedleHash == needleHash and haystack[(i - (len(needle) - 1)):(i + 1)] == needle:
return (i - (len(needle) - 1))
return -1
def hashFunction(self, char):
return ord(char) * 105943