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 and s[i-1] == c) or (i+1 < len(s) and s[i+1] == c):
continue
s[i] = c
break
return "".join(s)
class Solution {
public:
string modifyString(string s) {
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '?') {
for (char c = 'a'; c <= 'c'; ++c) {
if ((i > 0 && s[i-1] == c) || (i+1 < s.size() && s[i+1] == c))
continue;
s[i] = c;
break;
}
}
}
return s;
}
};
class Solution {
public String modifyString(String s) {
char[] arr = s.toCharArray();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == '?') {
for (char c = 'a'; c <= 'c'; c++) {
if ((i > 0 && arr[i-1] == c) || (i+1 < arr.length && arr[i+1] == c))
continue;
arr[i] = c;
break;
}
}
}
return new String(arr);
}
}
var modifyString = function(s) {
let arr = s.split('');
for (let i = 0; i < arr.length; i++) {
if (arr[i] === '?') {
for (let c of ['a', 'b', 'c']) {
if ((i > 0 && arr[i-1] === c) || (i+1 < arr.length && arr[i+1] === c))
continue;
arr[i] = c;
break;
}
}
}
return arr.join('');
};
Given a string s
consisting of lowercase English letters and the character '?'
, your task is to replace every '?'
in s
with a lowercase English letter so that no two consecutive characters are the same. Return any valid string after all replacements.
'?'
in the string.'?'
.
At first glance, the brute-force approach would be to try every possible letter for every '?'
and check if the resulting string has consecutive repeating characters. However, this is inefficient because the number of combinations grows exponentially with the number of '?'
characters.
The optimization comes from realizing that for each '?'
, we only need to ensure that the replacement character is different from its immediate neighbors (the previous and next characters). Since there are 26 letters, and at most two neighbors, we can always pick a letter that is not equal to either neighbor. We do not need to consider the entire string's context, only the local context of each '?'
.
Let's break down the solution step by step:
'?'
is encountered:
'?'
with it and move to the next character.This approach is efficient because:
'?'
.
Let's consider the input s = "a?b??a"
.
['a', '?', 'b', '?', '?', 'a']
.
'?'
['a', 'c', 'b', '?', '?', 'a']
.'?'
['a', 'c', 'b', 'a', '?', 'a']
.'?'
['a', 'c', 'b', 'a', 'b', 'a']
."acbaba"
, which has no consecutive repeating characters.
'?'
, try all 26 possible replacements, and for each combination, check the entire string for consecutive repeats. If there are k
question marks, this is O(26^k \cdot n)
, which is infeasible for large k
.
'?'
we try at most three candidates ('a', 'b', 'c'). Thus, the time complexity is O(n)
, where n
is the length of the string.
O(n)
extra space if we convert the string to a mutable array, otherwise O(1)
if in-place mutation is possible.
The key insight in this problem is that each '?'
only needs to differ from its immediate neighbors, so we can greedily pick a valid letter for each one. This local decision ensures the global constraint (no consecutive repeats) is satisfied, and the use of just three letters guarantees a solution exists. The algorithm is efficient, simple, and easy to implement in any language.