Permalink
Browse files

check to make sure string is longer than substring

  • Loading branch information...
1 parent a5dc90c commit a2d655d4a21542e1029f780ff75f81570fd9364b @jabagawee jabagawee committed Sep 29, 2012
Showing with 4 additions and 1 deletion.
  1. +4 −1 algorithms/searching/kmp_search.py
@@ -22,10 +22,13 @@
def search(string, word):
word_length = len(word)
+ string_length = len(string)
prefix = compute_prefix(word)
offsets = []
+ if word_length > string_length:
+ return offsets
q = 0 # q is the number of characters matched
- for i in xrange(len(string)):
+ for i in xrange(string_length):
while q > 0 and word[q] != string[i]:
q = prefix[q - 1] # next character does not match
if word[q] == string[i]:

0 comments on commit a2d655d

Please sign in to comment.