在 Python 中查找最长真棒子字符串的程序
假设我们有一个数字字符串s。正如我们所知,一个很棒的子串是s的一个非空子串,这样我们可以进行任意数量的交换以使其成为回文。我们必须找到s的最大长度awesome子串的长度。
因此,如果输入类似于s="4353526",那么输出将是5,因为"35352"是最长的真棒子字符串。我们可以制作“35253”回文。
示例
让我们看下面的实现来更好地理解
def solve(s):
n = 0
pos_map = {0:len(s)}
max_len = 1
for i in range(len(s)-1, -1, -1):
n = n ^ (1 << int(s[i]))
if n in pos_map:
max_len = max(max_len, pos_map[n]-i)
for j in range(10):
m = n ^ (1 << j)
if m in pos_map:
max_len = max(max_len, pos_map[m]-i)
if n not in pos_map:
pos_map[n] = i
return max_len
s = "4353526"
print(solve(s))输入
"4353526"输出结果
5
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短