2025-08-03 13:36:16 +0000 UTC

Replace All ?’s to Avoid Consecutive Repeating Characters

Code

class Solution:
    def modifyString(self, s: str) -> str:
        res = list(s)
        length = len(s)

        for i in range(length):
            char = res[i]
            if char != "?":
                continue
            for char_ascii in string.ascii_lowercase:
                if (
                    (i > 0 and res[i - 1] == char_ascii) 
                    or (i < length - 1 and res[i + 1] == char_ascii)
                ):
                    continue
                res[i] = char_ascii
                break
        return "".join(res)