forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1576.java
28 lines (26 loc) · 963 Bytes
/
_1576.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.fishercoder.solutions;
public class _1576 {
public static class Solution1 {
/**
* Each char could have at most two neighbors, so we only need to toggle between three character candidates to avoid repetition.
*/
public String modifyString(String s) {
char[] arr = s.toCharArray();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == '?') {
for (int j = 0; j < 3; j++) {
if (i > 0 && arr[i - 1] == 'a' + j) {
continue;
} else if (i < arr.length - 1 && arr[i + 1] == 'a' + j) {
continue;
} else {
arr[i] = (char) ('a' + j);
break;
}
}
}
}
return String.valueOf(arr);
}
}
}