Skip to content

Instantly share code, notes, and snippets.

@voith
Created December 14, 2019 10:41
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 voith/d8f639615118fd0e085c37c6674db41a to your computer and use it in GitHub Desktop.
Save voith/d8f639615118fd0e085c37c6674db41a to your computer and use it in GitHub Desktop.
def get_all_substrings(arr, index, subarr, record):
if index == len(arr):
if len(subarr) != 0:
record.append(''.join(subarr))
else:
get_all_substrings(arr, index + 1, subarr, record)
get_all_substrings(arr, index + 1, subarr + [arr[index]], record)
return record
def check(initial, target):
substrings = get_all_substrings(initial, 0, [], [])
length = len(target)
counter = 0
index = 0
reverse_idx = length
while reverse_idx != index:
if target[index:reverse_idx] in substrings:
counter += 1
index = reverse_idx
reverse_idx = length
else:
reverse_idx -= 1
if reverse_idx != length and reverse_idx == index:
return -1
else:
return counter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment