[英]Return the string having repetitive substrings consecutively more than x times
我想獲得連續重復子字符串超過 x 次的字符串。 子字符串有超過 y 個字符。 例如,當 x=4,y=3 時, BGGEFGEFGEFGEFFD
滿足條件(= GEF * 4 連續)。 另一方面,當x=2,y=4時, GDCCCGDCCFDCC
不滿足條件,因為GDCC之間有一個C。 有什么建議可以在不導入包的情況下檢查給定的字符串是否滿足條件? 提前致謝。
這是我嘗試過的
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’)
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)
使用字典存儲子字符串的計數信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.