Created
December 18, 2019 15:09
-
-
Save danieltanfh95/174574f4219bb9cb344c9cc73fb0befd to your computer and use it in GitHub Desktop.
findSmallestDivisor
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 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