Replace All ?'s to Avoid Consecutive Repeating Characters LeetCode solution in C++

1
class Solution {
2
char update_char(char x, char y) {
3
char c = 'a';
4
while (c == x || c == y) {
5
c++;
6
}
7
return c;
8
}
9

10
public:
11
string modifyString(string s) {
12
int l = s.length();
13
char c;
14
if (s[0] == '?') {
15
c = update_char(s[0], s[1]);
16
s[0] = c;
17
}
18
if (s[l - 1] == '?') {
19
c = update_char(s[l - 2], s[l - 1]);
20
s[l - 1] = c;
21
}
22
for (int i = 1; i < l - 1; ++i) {
23
if (s[i] == '?') {
24
c = update_char(s[i - 1], s[i + 1]);
25
s[i] = c;
26
}
27
}
28
return s;
29
}
30
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0