Skip to content

Instantly share code, notes, and snippets.

@ronzhin-dmitry
Created January 30, 2023 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronzhin-dmitry/af9b91c6c1925a7d7adaeff4fb7a332b to your computer and use it in GitHub Desktop.
Save ronzhin-dmitry/af9b91c6c1925a7d7adaeff4fb7a332b to your computer and use it in GitHub Desktop.
def z_function(input_string, result): #memory pre-allocated
n = len(input_string)
left,right = 0,0 #we have two pointers which will slide through array, right will always show the border of now-known prefix
for i in range(1,len(input_string)):
result[i] = max(0, min(right-i, result[i-left])) #init with known prefix info
while i + result[i] < n and input_string[result[i]] == input_string[i + result[i]]:
result[i] += 1 #run to the right to check prefix-equality
if i + result[i] > right: #if border moved - update left and right
left = i
right = i + result[i]
return
def search_z_func(needle, haystack, z_array):
z_function(needle+'$'+haystack, z_array) #using symbol '$' as a separator (suppose we have no '$' in text)
result = []
n = len(needle)
for i in range(len(needle), len(z_array)):
if z_array[i] == n:
result.append(i - len(needle) - 1) #if we found value of z-func equal to value of needle - save position
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment