LeetCode力扣打卡之1576. 替换所有的问号(Python3)

题目
在这里插入图片描述

逻辑是"?“的前一个元素和后一个元素不可能同时等于三个不同字母
所以遍历任意三个不同字母
取与前后元素不相同的那个替换”?"就行

为了方便判断边界元素 在两边添加一个空字符 代替前后元素

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]

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>