Skip to content

Instantly share code, notes, and snippets.

@PMExtra
Last active April 29, 2022 16:56
Show Gist options
  • Save PMExtra/a2dc4b9ab672250201341f077d2053b5 to your computer and use it in GitHub Desktop.
Save PMExtra/a2dc4b9ab672250201341f077d2053b5 to your computer and use it in GitHub Desktop.
def solution(s):
if not s: return 0
result = 1
min_divisor = 2
while True:
length = len(s)
factor = 1
# loop in a preprocessed prime number list can improve performance
for n in range(min_divisor, length + 1):
if length % n: continue
chunk_size = length // n
chunks = [s[i:i + chunk_size] for i in range(0, length, chunk_size)]
if chunks.count(chunks[0]) != len(chunks): continue
s = chunks[0]
min_divisor = n
factor = n
break
if factor == 1: break
result *= factor
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment