Skip to content

Commit 1ec8ae5

Browse files
add 1758
1 parent f0e6e94 commit 1ec8ae5

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-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+
|1758|[Minimum Changes To Make Alternating Binary String](https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1758.java) ||Easy|Greedy, Array|
1112
|1754|[Largest Merge Of Two Strings](https://leetcode.com/problems/largest-merge-of-two-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1753.java) ||Medium|Greedy, Suffix Array|
1213
|1753|[Maximum Score From Removing Stones](https://leetcode.com/problems/maximum-score-from-removing-stones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1753.java) ||Medium|Math, Heap|
1314
|1752|[Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1752.java) ||Easy|Array|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1758 {
4+
public static class Solution1 {
5+
public int minOperations(String s) {
6+
int ops1 = 0;
7+
//start with 0
8+
boolean isZero = true;
9+
for (int i = 0; i < s.length(); i++) {
10+
if (i % 2 == 0) {
11+
//should be zero, if not, change it to zero and increase ops1 by one
12+
if (s.charAt(i) != '0') {
13+
ops1++;
14+
}
15+
} else {
16+
//should be one, if not, increase ops1 by one
17+
if (s.charAt(i) != '1') {
18+
ops1++;
19+
}
20+
}
21+
}
22+
23+
//start with 1
24+
int ops2 = 0;
25+
for (int i = 0; i < s.length(); i++) {
26+
if (i % 2 == 0) {
27+
//should be one, if not, increase ops2 by one
28+
if (s.charAt(i) != '1') {
29+
ops2++;
30+
}
31+
} else {
32+
if (s.charAt(i) != '0') {
33+
ops2++;
34+
}
35+
}
36+
}
37+
return Math.min(ops1, ops2);
38+
}
39+
}
40+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1758;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1758Test {
10+
private static _1758.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1758.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.minOperations("0100"));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)