題目

邏輯是"?“的前一個元素和后一個元素不可能同時等于三個不同字母
所以遍歷任意三個不同字母
取與前后元素不相同的那個替換”?"就行
為了方便判斷邊界元素 在兩邊添加一個空字符 代替前后元素
class Solution:
def modifyString(self, s: str) -> str:
s = " "+s+" "
for index in range(1, len(s)-1):
if s[index] == "?" and s[index] != " ":
for c in ["a", "b", "c"]:
if s[index-1] != c and s[index+1] != c:
s = s.replace("?", c, 1)
break
return s[1: -1]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/404073.html
標籤:其他
