Skip to content

Commit c060665

Browse files
add 2609
1 parent 5a313b6 commit c060665

File tree

3 files changed

+56
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+56
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
| 2643 | [Row With Maximum Ones](https://leetcode.com/problems/row-with-maximum-ones/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2643.java) | | Easy |
2323
| 2641 | [Cousins in Binary Tree II](https://leetcode.com/problems/cousins-in-binary-tree-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2641.java) | | Medium |Tree, BFS, HashTable
2424
| 2605 | [Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2605.java) | | Easy |
25+
| 2609 | [Find the Longest Balanced Substring of a Binary String](https://leetcode.com/problems/find-the-longest-balanced-substring-of-a-binary-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2609.java) | | Easy |
2526
| 2600 | [K Items With the Maximum Sum](https://leetcode.com/problems/k-items-with-the-maximum-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2600.java) | | Easy |
2627
| 2596 | [Check Knight Tour Configuration](https://leetcode.com/problems/check-knight-tour-configuration/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2596.java) | [:tv:](https://youtu.be/OBht8NT_09c) | Medium |
2728
| 2595 | [Number of Even and Odd Bits](https://leetcode.com/problems/number-of-even-and-odd-bits/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2595.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
public class _2609 {
4+
public static class Solution1 {
5+
public int findTheLongestBalancedSubstring(String s) {
6+
int longest = 0;
7+
for (int i = 0; i < s.length(); i++) {
8+
if (s.charAt(i) == '0') {
9+
int zeroes = 0;
10+
while (i < s.length() && s.charAt(i) == '0') {
11+
i++;
12+
zeroes++;
13+
}
14+
if (i < s.length()) {
15+
int ones = 0;
16+
while (i < s.length() && s.charAt(i) == '1') {
17+
i++;
18+
ones++;
19+
}
20+
longest = Math.max(longest, Math.min(ones, zeroes) * 2);
21+
i--;
22+
}
23+
}
24+
}
25+
return longest;
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2609;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _2609Test {
10+
private static _2609.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2609.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(6, solution1.findTheLongestBalancedSubstring("01000111"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(2, solution1.findTheLongestBalancedSubstring("001"));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)