题解 | #小红的字符串构造#
小红的字符串构造
https://www.nowcoder.com/practice/3e4b4dabc2e444e384c3ae62ac7dd84e
s = list(input())
n = len(s)
dic = sorted(set(s))
t = [-1]*n
s1 = len(dic)
for i in range(n):
t[i] = dic[(dic.index(s[i]) + 1)%s1]
if s1 > 1:
print(''.join(t))
else:
print(-1)
使用set去重得到原串字符集,随后构造t串,可以使用index函数查找s串中对应字符在字符集中的下标,+1后对字符集大小取模,即可保证与s串不同
