Skip to content

Instantly share code, notes, and snippets.

@danieltanfh95
Created December 18, 2019 15:09
Show Gist options
  • Save danieltanfh95/174574f4219bb9cb344c9cc73fb0befd to your computer and use it in GitHub Desktop.
Save danieltanfh95/174574f4219bb9cb344c9cc73fb0befd to your computer and use it in GitHub Desktop.
findSmallestDivisor
def findSmallestDivisor(s,t):
if(check_if_divisible(s, t)):
return len(find_shortest_repeating_substring(t))
else:
return -1
def check_if_divisible(s, t):
is_length_divisible = (len(s) % len(t)) == 0
if(is_length_divisible):
return t*(len(s)//len(t)) == s
def find_shortest_repeating_substring(string):
for x in range(1, len(string)):
substring = string[:x]
if substring * (len(string)//len(substring)) == string:
return substring
return string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment