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.
Input: s ="?zs"Output: "azs"Explanation: There are 25 solutions forthis 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".
Input: s ="ubv?w"Output: "ubvaw"Explanation: There are 24 solutions forthis problem. Only "v" and "w" are invalid modifications as the strings will consist of consecutive repeating characters in"ubvvw" and "ubvww".
publicclassSolution {
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;
}
}
}
}
returnnew String(arr);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
funmodifyString(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)
}
1
2
3
4
5
6
7
8
9
10
defmodifyString(s: str) -> str:
arr = list(s)
n = len(arr)
for i in range(n):
if arr[i] =='?':
for c in'abc':
if (i ==0or arr[i-1] != c) and (i == n-1or arr[i+1] != c):
arr[i] = c
breakreturn''.join(arr)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fnmodify_string(s: &str) -> String {
letmut arr: Vec<char>= s.chars().collect();
let n = arr.len();
for i in0..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()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
functionmodifyString(s: string):string {
constarr=s.split("");
for (leti=0; i<arr.length; i++) {
if (arr[i] ==='?') {
for (constcof ['a', 'b', 'c']) {
if ((i===0||arr[i-1] !==c) && (i===arr.length-1||arr[i+1] !==c)) {
arr[i] =c;
break;
}
}
}
}
returnarr.join("");
}