'Return the string having repetitive substrings consecutively more than x times

I’d like to get the string having repetitive substrings consecutively more than x times. The substrings has more than y characters. For example, when x=4, y=3, BGGEFGEFGEFGEFFD satisfies the condition (= GEF * 4 consecutively). On the other hand, when x=2, y=4, GDCCCGDCCFDCC does not satisfies the condition since there is one C between GDCC. Any suggestions to check if the given string satisfies the condition without importing packages? Thanks in advance.

This is what I’ve tried

counter = 0
for s in range(0, len(string),y):
    if string[s : s+y] == string[s+y :s+y*2]:
       counter +=1
if counter >= x:
   print(‘TRUE’)



Solution 1:[1]

def is_expected_str(str_val, x, y):
    match_found = False
    split_strs = {}
    index = 0
    for i in str_val:
        subs = str_val[index:(index + y)]
        index = index + 1
        if len(subs) < y:
            continue

        sub_count = split_strs.get(subs)

        if sub_count is None:
            match_count = 1
        else:
            match_count = split_strs[subs] + 1

        split_strs[subs] = match_count

        if match_count >= x:
            match_found = True

    print(split_strs)
    return match_found
    # print(subs)


rr = "BGGEFGEFGEFGEFFD"
res = is_expected_str(rr, x=4, y=3)
print(res)

Use dictionary to store the count information of sub strings

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nandha