Skip to content

Commit 0fc8146

Browse files
committed
Added Replace All ?'s to Avoid Consecutive Repeating Characters.java
1 parent 01af7f3 commit 0fc8146

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
private final char[] ALL_CHARACTERS = "abcdefghijklmnopqrstuvwxyz".toCharArray();
3+
public String modifyString(String s) {
4+
char[] charArray = s.toCharArray();
5+
for (int idx = 0; idx < charArray.length; idx++) {
6+
if (charArray[idx] == '?') {
7+
charArray[idx] = getNonRepeatingChar(charArray, idx);
8+
}
9+
}
10+
return String.valueOf(charArray);
11+
}
12+
13+
private char getNonRepeatingChar(char[] charArray, int idx) {
14+
for (char c : ALL_CHARACTERS) {
15+
if ((idx == 0 || charArray[idx - 1] == '?' || charArray[idx - 1] != c) &&
16+
(idx == charArray.length - 1 || charArray[idx + 1] == '?' || charArray[idx + 1] != c)) {
17+
return c;
18+
}
19+
}
20+
return charArray[idx];
21+
}
22+
}

0 commit comments

Comments
 (0)