I'm trying to write a function that takes in a string and spits out the number of repeated patterns:
string1="abcabc"
string 2="abcdabcdabcd"
solution(string1)=2
solution(string2)=3
My code is below. It works for most cases but I'm still failing a hidden test case (last one out of 10)
def solution(s):
final_score=[]
for x in range(1,50,1):
pattern=s[0:x]
repeats=[(s[i:i+x]) for i in range(x,len(s),x)]
#print(pattern,repeats)
if all(pattern in x for x in repeats):
#print(len(repeats))
final_score.append(len(repeats)+1)
else:
continue
#print(final_score)
return(max(final_score))
Any advice would be much appreciated, thanks!
Edit: For the case of "abababab", or where multiple patterns are available ("ab" and "abab"), I'm trying to return the highest frequency (in this case, "ab" repeats 4 times, so the function should return 4)