在一行上输入一个长度为
且为偶数,仅由小写字母构成的字符串
,代表待修改的字符串。
输出一个整数,表示将
修改为双生串的最小修改次数。
popipa
3
在这个样例中,将
修改为
是其中一个最优解。
aaaa
0
在这个样例中,给定的字符串已经是双生串,不需要修改。
s = input() n = int(len(s)/2) s1 = s[:n] s2 = s[n:] x = set(s1) y = set(s2) max1= max([s1.count(i) for i in x]) max2= max([s2.count(j) for j in y]) p = (n-max1) + (n-max2) print(p)
from collections import Counter s = input() s_len = len(s) left_half_counter = Counter(s[:s_len // 2]) right_half_counter = Counter(s[s_len // 2:]) # 左边 left_edit_times = s_len // 2 - left_half_counter.most_common(1)[0][1] right_edit_times = s_len // 2 - right_half_counter.most_common(1)[0][1] print(left_edit_times + right_edit_times)