Skip to content

Commit 06b5ac6

Browse files
add 1576
1 parent 468013a commit 06b5ac6

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1576.java) ||String|Easy|
1112
|1574|[Shortest Subarray to be Removed to Make Array Sorted](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1574.java) ||Array, Binary Search|Medium|
1213
|1572|[Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1572.java) ||Array|Easy|
1314
|1567|[Maximum Length of Subarray With Positive Product](https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1567.java) |[:tv:](https://youtu.be/bFer5PdsgpY)|Greedy|Medium|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1576 {
4+
public static class Solution1 {
5+
public String modifyString(String s) {
6+
StringBuilder sb = new StringBuilder();
7+
for (int i = 0; i < s.length(); i++) {
8+
char c = s.charAt(i);
9+
if (c == '?') {
10+
char replacement = findReplacement(sb, i, s);
11+
sb.append(replacement);
12+
} else {
13+
sb.append(c);
14+
}
15+
}
16+
return sb.toString();
17+
}
18+
19+
private char findReplacement(StringBuilder sb, int index, String s) {
20+
char replacement = 'a';
21+
if (index > 0) {
22+
int count = 1;
23+
while (replacement == sb.charAt(index - 1)) {
24+
replacement += count % 26;
25+
}
26+
}
27+
if (index + 1 < s.length()) {
28+
int count = 1;
29+
while (replacement == s.charAt(index + 1) || (index > 0 && replacement == sb.charAt(index - 1))) {
30+
replacement += count % 26;
31+
}
32+
}
33+
return replacement;
34+
}
35+
}
36+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1576;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1576Test {
10+
private static _1576.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1576.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("azs", solution1.modifyString("?zs"));
20+
}
21+
}

0 commit comments

Comments
 (0)