code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

1221 Split A String In Balanced Strings

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/*
 * Balanced strings are those who have equal quantity of 'L' and 'R' characters.
 * Given a balanced string s split it in the maximum amount of balanced strings.
 * Return the maximum amount of splitted balanced strings.
 *
 * Example 1:
 * Input: s = "RLRRLLRLRL"
 * Output: 4
 * Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring
 * contains same number of 'L' and 'R'.
 */

function SplitAStringInBalancedStrings(value) {
  let l = 0,
    r = 0,
    c = 0
  for (let index = 0; index < value.length; index++) {
    if (value[index] === 'L') {
      l++
    } else {
      r++
    }
    if (l === r) {
      c++
    }
  }
  return c
}

test('Split A String In Balanced Strings', () => {
  expect(SplitAStringInBalancedStrings('RLRRLLRLRL')).toEqual(4)
})

Created 2020-04-20T13:23:23+00:00 · Edit