|
| 1 | +# [2609. 最长平衡子字符串](https://leetcode.cn/problems/find-the-longest-balanced-substring-of-a-binary-string) |
| 2 | + |
| 3 | +[English Version](/solution/2600-2699/2609.Find%20the%20Longest%20Balanced%20Substring%20of%20a%20Binary%20String/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>给你一个仅由 <code>0</code> 和 <code>1</code> 组成的二进制字符串 <code>s</code> 。<span style=""> </span><span style=""> </span></p> |
| 10 | + |
| 11 | +<p>如果子字符串中 <strong>所有的<span style=""> </span></strong><code><span style="">0</span></code><strong><span style=""> </span>都在 </strong><code>1</code><strong> 之前</strong> 且其中 <code>0</code> 的数量等于 <code>1</code> 的数量,则认为 <code>s</code> 的这个子字符串是平衡子字符串。请注意,空子字符串也视作平衡子字符串。<span style=""> </span></p> |
| 12 | + |
| 13 | +<p>返回 <span style=""> </span><code>s</code> 中最长的平衡子字符串长度。</p> |
| 14 | + |
| 15 | +<p>子字符串是字符串中的一个连续字符序列。</p> |
| 16 | + |
| 17 | +<p> </p> |
| 18 | + |
| 19 | +<p><strong>示例 1:</strong></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | +<strong>输入:</strong>s = "01000111" |
| 23 | +<strong>输出:</strong>6 |
| 24 | +<strong>解释:</strong>最长的平衡子字符串是 "000111" ,长度为 6 。 |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p><strong>示例 2:</strong></p> |
| 28 | + |
| 29 | +<pre> |
| 30 | +<strong>输入:</strong>s = "00111" |
| 31 | +<strong>输出:</strong>4 |
| 32 | +<strong>解释:</strong>最长的平衡子字符串是 "0011" ,长度为 <span style=""> </span>4 。 |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><strong>示例 3:</strong></p> |
| 36 | + |
| 37 | +<pre> |
| 38 | +<strong>输入:</strong>s = "111" |
| 39 | +<strong>输出:</strong>0 |
| 40 | +<strong>解释:</strong>除了空子字符串之外不存在其他平衡子字符串,所以答案为 0 。 |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | + |
| 45 | +<p><strong>提示:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li><code>1 <= s.length <= 50</code></li> |
| 49 | + <li><code>'0' <= s[i] <= '1'</code></li> |
| 50 | +</ul> |
| 51 | + |
| 52 | +## 解法 |
| 53 | + |
| 54 | +<!-- 这里可写通用的实现逻辑 --> |
| 55 | + |
| 56 | +<!-- tabs:start --> |
| 57 | + |
| 58 | +### **Python3** |
| 59 | + |
| 60 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 61 | + |
| 62 | +```python |
| 63 | +class Solution: |
| 64 | + def findTheLongestBalancedSubstring(self, s: str) -> int: |
| 65 | + def check(i, j): |
| 66 | + cnt = 0 |
| 67 | + for k in range(i, j + 1): |
| 68 | + if s[k] == '1': |
| 69 | + cnt += 1 |
| 70 | + elif cnt: |
| 71 | + return False |
| 72 | + return cnt * 2 == (j - i + 1) |
| 73 | + |
| 74 | + n = len(s) |
| 75 | + ans = 0 |
| 76 | + for i in range(n): |
| 77 | + for j in range(i + 1, n): |
| 78 | + if check(i, j): |
| 79 | + ans = max(ans, j - i + 1) |
| 80 | + return ans |
| 81 | +``` |
| 82 | + |
| 83 | +### **Java** |
| 84 | + |
| 85 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 86 | + |
| 87 | +```java |
| 88 | +class Solution { |
| 89 | + public int findTheLongestBalancedSubstring(String s) { |
| 90 | + int n = s.length(); |
| 91 | + int ans = 0; |
| 92 | + for (int i = 0; i < n; ++i) { |
| 93 | + for (int j = i + 1; j < n; ++j) { |
| 94 | + if (check(s, i, j)) { |
| 95 | + ans = Math.max(ans, j - i + 1); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return ans; |
| 100 | + } |
| 101 | + |
| 102 | + private boolean check(String s, int i, int j) { |
| 103 | + int cnt = 0; |
| 104 | + for (int k = i; k <= j; ++k) { |
| 105 | + if (s.charAt(k) == '1') { |
| 106 | + ++cnt; |
| 107 | + } else if (cnt > 0) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + return cnt * 2 == j - i + 1; |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### **C++** |
| 117 | + |
| 118 | +```cpp |
| 119 | +class Solution { |
| 120 | +public: |
| 121 | + int findTheLongestBalancedSubstring(string s) { |
| 122 | + int n = s.size(); |
| 123 | + int ans = 0; |
| 124 | + auto check = [&](int i, int j) -> bool { |
| 125 | + int cnt = 0; |
| 126 | + for (int k = i; k <= j; ++k) { |
| 127 | + if (s[k] == '1') { |
| 128 | + ++cnt; |
| 129 | + } else if (cnt) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + } |
| 133 | + return cnt * 2 == j - i + 1; |
| 134 | + }; |
| 135 | + for (int i = 0; i < n; ++i) { |
| 136 | + for (int j = i + 1; j < n; ++j) { |
| 137 | + if (check(i, j)) { |
| 138 | + ans = max(ans, j - i + 1); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + return ans; |
| 143 | + } |
| 144 | +}; |
| 145 | +``` |
| 146 | +
|
| 147 | +### **Go** |
| 148 | +
|
| 149 | +```go |
| 150 | +func findTheLongestBalancedSubstring(s string) (ans int) { |
| 151 | + n := len(s) |
| 152 | + check := func(i, j int) bool { |
| 153 | + cnt := 0 |
| 154 | + for k := i; k <= j; k++ { |
| 155 | + if s[k] == '1' { |
| 156 | + cnt++ |
| 157 | + } else if cnt > 0 { |
| 158 | + return false |
| 159 | + } |
| 160 | + } |
| 161 | + return cnt*2 == j-i+1 |
| 162 | + } |
| 163 | + for i := 0; i < n; i++ { |
| 164 | + for j := i + 1; j < n; j++ { |
| 165 | + if check(i, j) { |
| 166 | + ans = max(ans, j-i+1) |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + return |
| 171 | +} |
| 172 | +
|
| 173 | +func max(a, b int) int { |
| 174 | + if a > b { |
| 175 | + return a |
| 176 | + } |
| 177 | + return b |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +### **...** |
| 182 | + |
| 183 | +``` |
| 184 | +
|
| 185 | +``` |
| 186 | + |
| 187 | +<!-- tabs:end --> |
0 commit comments