1221:分割平衡字符串
This commit is contained in:
parent
bfa9fe0e86
commit
ed0001536e
@ -0,0 +1,84 @@
|
|||||||
|
//在一个 平衡字符串 中,'L' 和 'R' 字符的数量是相同的。
|
||||||
|
//
|
||||||
|
// 给你一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。
|
||||||
|
//
|
||||||
|
// 注意:分割得到的每个字符串都必须是平衡字符串。
|
||||||
|
//
|
||||||
|
// 返回可以通过分割得到的平衡字符串的 最大数量 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:s = "RLRRLLRLRL"
|
||||||
|
//输出:4
|
||||||
|
//解释:s 可以分割为 "RL"、"RRLL"、"RL"、"RL" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:s = "RLLLLRRRLR"
|
||||||
|
//输出:3
|
||||||
|
//解释:s 可以分割为 "RL"、"LLLRRR"、"LR" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:s = "LLLLRRRR"
|
||||||
|
//输出:1
|
||||||
|
//解释:s 只能保持原样 "LLLLRRRR".
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 4:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:s = "RLRRRLLRLL"
|
||||||
|
//输出:2
|
||||||
|
//解释:s 可以分割为 "RL"、"RRRLLRLL" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= s.length <= 1000
|
||||||
|
// s[i] = 'L' 或 'R'
|
||||||
|
// s 是一个 平衡 字符串
|
||||||
|
//
|
||||||
|
// Related Topics 贪心 字符串 计数 👍 143 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//1221:分割平衡字符串
|
||||||
|
class SplitAStringInBalancedStrings {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new SplitAStringInBalancedStrings().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int balancedStringSplit(String s) {
|
||||||
|
int num = 0;
|
||||||
|
int count = 0;
|
||||||
|
for (char ch : s.toCharArray()) {
|
||||||
|
if (ch == 'L') {
|
||||||
|
num++;
|
||||||
|
} else {
|
||||||
|
num--;
|
||||||
|
}
|
||||||
|
if (num == 0) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
<p>在一个 <strong>平衡字符串</strong> 中,<code>'L'</code> 和 <code>'R'</code> 字符的数量是相同的。</p>
|
||||||
|
|
||||||
|
<p>给你一个平衡字符串 <code>s</code>,请你将它分割成尽可能多的平衡字符串。</p>
|
||||||
|
|
||||||
|
<p><strong>注意:</strong>分割得到的每个字符串都必须是平衡字符串。</p>
|
||||||
|
|
||||||
|
<p>返回可以通过分割得到的平衡字符串的 <strong>最大数量</strong> <strong>。</strong></p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = "RLRRLLRLRL"
|
||||||
|
<strong>输出:</strong>4
|
||||||
|
<strong>解释:</strong>s 可以分割为 "RL"、"RRLL"、"RL"、"RL" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = "RLLLLRRRLR"
|
||||||
|
<strong>输出:</strong>3
|
||||||
|
<strong>解释:</strong>s 可以分割为 "RL"、"LLLRRR"、"LR" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = "LLLLRRRR"
|
||||||
|
<strong>输出:</strong>1
|
||||||
|
<strong>解释:</strong>s 只能保持原样 "LLLLRRRR".
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 4:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = "RLRRRLLRLL"
|
||||||
|
<strong>输出:</strong>2
|
||||||
|
<strong>解释:</strong>s 可以分割为 "RL"、"RRRLLRLL" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= s.length <= 1000</code></li>
|
||||||
|
<li><code>s[i] = 'L' 或 'R'</code></li>
|
||||||
|
<li><code>s</code> 是一个 <strong>平衡</strong> 字符串</li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>贪心</li><li>字符串</li><li>计数</li></div></div><br><div><li>👍 143</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user