LeetCode 题解工作台

分割平衡字符串

平衡字符串 中, 'L' 和 'R' 字符的数量是相同的。 给你一个平衡字符串 s ,请你将它分割成尽可能多的子字符串,并满足: 每个子字符串都是平衡字符串。 返回可以通过分割得到的平衡字符串的 最大数量 。 示例 1: 输入: s = "RLRRLLRLRL" 输出: 4 解释: s 可以分割为 …

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

简单 · 贪心·invariant

bolt

答案摘要

我们用变量 维护当前字符串的平衡度,即 的值为当前字符串中 的数量减去 的数量。当 的值为 0 时,我们就找到了一个平衡字符串。 遍历字符串 ,当遍历到第 个字符时,如果 $s[i] = L$,则 的值加 1,否则 的值减 1。当 的值为 0 时,我们将答案加 1。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 贪心·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

平衡字符串 中,'L''R' 字符的数量是相同的。

给你一个平衡字符串 s,请你将它分割成尽可能多的子字符串,并满足:

  • 每个子字符串都是平衡字符串。

返回可以通过分割得到的平衡字符串的 最大数量

 

示例 1:

输入:s = "RLRRLLRLRL"
输出:4
解释:s 可以分割为 "RL"、"RRLL"、"RL"、"RL" ,每个子字符串中都包含相同数量的 'L' 和 'R' 。

示例 2:

输入:s = "RLRRRLLRLL"
输出:2
解释:s 可以分割为 "RL"、"RRRLLRLL",每个子字符串中都包含相同数量的 'L' 和 'R' 。
注意,s 无法分割为 "RL"、"RR"、"RL"、"LR"、"LL" 因为第 2 个和第 5 个子字符串不是平衡字符串。

示例 3:

输入:s = "LLLLRRRR"
输出:1
解释:s 只能保持原样 "LLLLRRRR" 。

 

提示:

  • 2 <= s.length <= 1000
  • s[i] = 'L' 或 'R'
  • s 是一个 平衡 字符串
lightbulb

解题思路

方法一:贪心

我们用变量 ll 维护当前字符串的平衡度,即 ll 的值为当前字符串中 LL 的数量减去 RR 的数量。当 ll 的值为 0 时,我们就找到了一个平衡字符串。

遍历字符串 ss,当遍历到第 ii 个字符时,如果 s[i]=Ls[i] = L,则 ll 的值加 1,否则 ll 的值减 1。当 ll 的值为 0 时,我们将答案加 1。

时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)。其中 nn 为字符串 ss 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def balancedStringSplit(self, s: str) -> int:
        ans = l = 0
        for c in s:
            if c == 'L':
                l += 1
            else:
                l -= 1
            if l == 0:
                ans += 1
        return ans
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for candidates who demonstrate a clear understanding of greedy algorithms and balancing.

  • question_mark

    Candidates should explain how the balance variable directly relates to substring splitting.

  • question_mark

    The solution should be efficient, ideally with linear time complexity.

warning

常见陷阱

外企场景
  • error

    Candidates may attempt to split the string based on arbitrary substrings without maintaining the balance correctly.

  • error

    Mistakes can occur when the balance is not reset correctly or if splits are made without checking the balance.

  • error

    Some may overcomplicate the problem by trying to split substrings explicitly instead of using a simple balance tracking method.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if the string contains additional characters besides 'L' and 'R'? This would change the problem entirely, making it unsuitable for this approach.

  • arrow_right_alt

    Consider the case where the string consists of only 'L's or only 'R's. This would lead to an invalid problem definition, as a balanced string requires both characters.

  • arrow_right_alt

    What happens when the input string is already split into balanced substrings? In this case, the algorithm should still work efficiently without any extra operations.

help

常见问题

外企场景

分割平衡字符串题解:贪心·invariant | LeetCode #1221 简单