LeetCode solutions

1698. Replace All ?'s to Avoid Consecutive Repeating Characters

My accepted Python solution to LeetCode problem 1698, Replace All ?'s to Avoid Consecutive Repeating Characters, running in 0ms.

  • Difficulty: Easy
  • Python
  • Runtime 0ms
  • Memory 17.1MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.1MB, accepted 2025-12-30.

python
class Solution:
    def modifyString(self, s: str) -> str:
        s = list(s)
        for i in range(len(s)):
            if s[i] == '?':
                for c in 'abc':
                    if (i == 0 or s[i-1] != c) and (i == len(s)-1 or s[i+1] != c):
                        s[i] = c
                        break
        return ''.join(s)

Source