def find_irreducible_prefix(brackets):
  """Find minimal prefix of string which is balanced (equal Ls and Rs).

  Args:
    brackets: A string containing an equal number of (and only) 'L's and 'R's.

  Returns:
    A two-element tuple.

    The first element is the minimal "irreducible prefix" of
    `brackets`, which is the shortest non-empty prefix of `brackets` which
    contains an equal number of 'L's and 'R's.

    The second element is the rest of the string.

  Raises:
    ValueError: No irreducible prefix could be found, or `brackets` was empty.
  """

  depth = 0
  for i, bracket in enumerate(brackets):
    depth += 1 if bracket == 'L' else -1
    if i > 0 and depth == 0:
      return brackets[:i + 1], brackets[i + 1:]

  raise ValueError('unbalanced or empty: %s' % ''.join(brackets))