i want to get all repeated substrings of a specific length in a string and their count. Example: string = "abcabcabcdabcd"
get_repeats(string, 3 #length of the substrings)
output:
abc (4)
bca (2)
cab (2)
i want to get all repeated substrings of a specific length in a string and their count. Example: string = "abcabcabcdabcd"
get_repeats(string, 3 #length of the substrings)
output:
abc (4)
bca (2)
cab (2)
from collections import Counter
def get_repeats(s, k):
ctr = Counter(s[i:i+k] for i in range(len(s) - k + 1))
return {sub: c for sub, c in ctr.items() if c > 1}
And usage:
>>> get_repeats("abcabcabcdabcd", 3)
{'abc': 4, 'bca': 2, 'cab': 2}