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

Posted 我不是秃头sheep

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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]

以上是关于LeetCode力扣打卡之1576. 替换所有的问号(Python3)的主要内容,如果未能解决你的问题,请参考以下文章