Skip to content

Latest commit

 

History

History
46 lines (38 loc) · 835 Bytes

1221.split-a-string-in-balanced-strings.md

File metadata and controls

46 lines (38 loc) · 835 Bytes

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

示例 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'

var balancedStringSplit = function (s) {
  let counter = 0;
  let i = 0,
    r = 0,
    l = 0;
  while (i < s.length) {
    if (s[i] == "L") l++;
    else r++;
    if (l == r) {
      counter++;
      r = 0;
      l = 0;
    }
    i++;
  }
  return counter;
};