704.二分查找
链接:704.二分查找
难度:Easy
标签:数组、二分查找
简介:返回可以通过分割得到的平衡字符串的 最大数量 。
题解 1 - typescript
- 编辑时间:2021-09-07
- 执行用时:64ms
- 内存消耗:39.7MB
- 编程语言:typescript
- 解法介绍:遍历,判断 rl 相等时。
function balancedStringSplit(s: string): number {
  let r = 0;
  let l = 0;
  let ans = 0;
  for (const c of s) {
    if (c === 'R') r++;
    if (c === 'L') l++;
    if (r === l) {
      ans++;
      r = l = 0;
    }
  }
  return ans;
}