'Creates a set partition from a restricted growth string.
The indices given in rgs are assumed to be the index
of the element as given in elements *as provided* (the
elements are not sorted by this routine). Block numbering
starts from 0. If any block was not referenced in ``rgs``
an error will be raised.
Examples
>>> from sympy.combinatorics.partitions import Partition
>>> Partition.from_rgs([0, 1, 2, 0, 1], list(\'abcde\'))
{{c}, {a, d}, {b, e}}
>>> Partition.from_rgs([0, 1, 2, 0, 1], list(\'cbead\'))
{{e}, {a, c}, {b, d}}
>>> a = Partition([1, 4], [2], [3, 5])
>>> Partition.from_rgs(a.RGS, a.members)
{{2}, {1, 4}, {3, 5}}'

@classmethod
def from_rgs(self, rgs, elements):

    if (len(rgs) != len(elements)):
        raise ValueError('mismatch    in    rgs    and    element    lengths')
    max_elem = (max(rgs) + 1)
    partition = [[] for i in range(max_elem)]
    j = 0
    for i in rgs:
        partition[i].append(elements[j])
        j += 1
    if (not all((p for p in partition))):
        raise ValueError('some    blocks    of    the    partition    were    empty.')
    return Partition(*partition)
