Created
December 14, 2019 10:41
-
-
Save voith/d8f639615118fd0e085c37c6674db41a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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