LeetCode

    科技2025-02-03  16

    package number5; //贪心算法 import java.util.Stack; //在一个「平衡字符串」中,'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". //提示: //1 <= s.length <= 1000 //s[i] = 'L' 或 'R' //分割得到的每个字符串都必须是平衡字符串。 public class Solution1 { public static void main(String[] args) { String s="LLLLRLRLRLRRRRRL"; System.out.println(balancedStringSplit(s)); } public static int balancedStringSplit(String s) { char[] a=s.toCharArray(); int cnt = 0; int balance = 0; for(int i = 0; i < a.length; i++){ if(a[i] == 'L') balance --; if(a[i] == 'R') balance ++; if(balance == 0) cnt ++; } return cnt; } }
    Processed: 0.011, SQL: 8