Replace All ?'s to Avoid Consecutive Repeating Characters
EasyUpdated: Aug 2, 2025
Practice on:
Problem
Given a string s containing only lowercase English letters and the '?'
character, convert all the '?' characters into lowercase letters such that the final string does not contain any consecutive repeating characters. You cannot modify the non '?' characters.
It is guaranteed that there are no consecutive repeating characters in the given string except for '?'.
Return the final string after all the conversions (possibly zero) have been made. If there is more than one solution, return any of them. It can be shown that an answer is always possible with the given constraints.
Examples
Example 1
Input: s = "?zs"
Output: "azs"
Explanation: There are 25 solutions for this problem. From "azs" to "yzs", all are valid. Only "z" is an invalid modification as the string will consist of consecutive repeating characters in "zzs".
Example 2
Input: s = "ubv?w"
Output: "ubvaw"
Explanation: There are 24 solutions for this problem. Only "v" and "w" are invalid modifications as the strings will consist of consecutive repeating characters in "ubvvw" and "ubvww".
Constraints
1 <= s.length <= 100sconsist of lowercase English letters and'?'.
Solution
Method 1 - Greedy Replacement
Intuition
For each '?', pick any letter different from its neighbors. Since the string is at most 100 characters, we can check 'a', 'b', 'c' for each '?'.
Approach
- Convert the string to a list for mutability.
- For each index i where s[i] == '?', try 'a', 'b', 'c' and pick the first that is not equal to s[i-1] or s[i+1] (if those exist).
- Return the joined string.
Code
C++
#include <string>
using namespace std;
string modifyString(string s) {
for (int i = 0; i < s.size(); ++i) {
if (s[i] == '?') {
for (char c = 'a'; c <= 'z'; ++c) {
if ((i == 0 || s[i-1] != c) && (i == s.size()-1 || s[i+1] != c)) {
s[i] = c;
break;
}
}
}
}
return s;
}
Go
func modifyString(s string) string {
arr := []byte(s)
for i := 0; i < len(arr); i++ {
if arr[i] == '?' {
for c := byte('a'); c <= 'z'; c++ {
if (i == 0 || arr[i-1] != c) && (i == len(arr)-1 || arr[i+1] != c) {
arr[i] = c
break
}
}
}
}
return string(arr)
}
Java
public 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 <= 'z'; c++) {
if ((i == 0 || arr[i-1] != c) && (i == arr.length-1 || arr[i+1] != c)) {
arr[i] = c;
break;
}
}
}
}
return new String(arr);
}
}
Kotlin
fun modifyString(s: String): String {
val arr = s.toCharArray()
for (i in arr.indices) {
if (arr[i] == '?') {
for (c in 'a'..'z') {
if ((i == 0 || arr[i-1] != c) && (i == arr.size-1 || arr[i+1] != c)) {
arr[i] = c
break
}
}
}
}
return String(arr)
}
Python
def modifyString(s: str) -> str:
arr = list(s)
n = len(arr)
for i in range(n):
if arr[i] == '?':
for c in 'abc':
if (i == 0 or arr[i-1] != c) and (i == n-1 or arr[i+1] != c):
arr[i] = c
break
return ''.join(arr)
Rust
fn modify_string(s: &str) -> String {
let mut arr: Vec<char> = s.chars().collect();
let n = arr.len();
for i in 0..n {
if arr[i] == '?' {
for c in ['a', 'b', 'c'].iter() {
if (i == 0 || arr[i-1] != *c) && (i == n-1 || arr[i+1] != *c) {
arr[i] = *c;
break;
}
}
}
}
arr.into_iter().collect()
}
TypeScript
function modifyString(s: string): string {
const arr = s.split("");
for (let i = 0; i < arr.length; i++) {
if (arr[i] === '?') {
for (const c of ['a', 'b', 'c']) {
if ((i === 0 || arr[i-1] !== c) && (i === arr.length-1 || arr[i+1] !== c)) {
arr[i] = c;
break;
}
}
}
}
return arr.join("");
}
Complexity
- ⏰ Time complexity: O(N), where N is the length of s.
- 🧺 Space complexity: O(N), for the output string.