

首先排除特殊情况,字符串长度为1且是‘?’,随意匹配一个小写字母。之后根据 '?' 出现在字符串中的位置分为三种情况:
1、'?'在字符串开头i=0,只需要和后一个元素不一样即可;
2、'?'在字符串末尾i=len(mylist)-1,只需要和前一个元素不一样即可;
3、'?'在中间,需要与两侧相邻的元素不一样。
代码实现如下:
def modifyString(s: str) -> str:
word = ["a", "b", "c"]
mylist = list(s)
n = len(mylist)
if n == 1 and mylist[0] == '?':
mylist[0] = word[0] # 只有一位且是‘?’的情况
else:
for i in range(n):
if i == 0 and mylist[i] == '?': # '?'出现在字符串开头的情况
for c in word:
if c != mylist[i+1]:
mylist[i] = c
break
elif i == n - 1 and mylist[i] == '?': # '?' 出现在字符串结尾的情况
for c in word:
if c != mylist[i-1]:
mylist[i] = c
break
else:
if mylist[i] == '?':
for c in word:
if c != mylist[i-1] and c != mylist[i+1]:
mylist[i] = c
return "".join(mylist)
s = "?zs"
print(modifyString(s)) # azsEND