User blog comment:LittlePeng9/Random Turing machines/@comment-5529393-20130408132904/@comment-1605058-20130410180513

Consider us checking parentheses ()() for rightmost balanced pair. We delete, or move, rightmost parenthesis (it always is ) ) and we start counting. Our count starts with 1, now we are at ()( ). Then is count 2 with ()(), we increase count with every ). For every ( we decrease count. We continue until count hits 0: ()( ) 1, ()() 2, ()( ) 1, () () 0. Now we need lemma to prove this is what we look for - our procedure gives rightmost set with same number of ('s and )'s. I can prove that if this set has ) on its right end it must be balanced. You can try yourself to prove it. If there were more-to-right balanced set, procedure would find it earlier, as balanced set has same number of ('s and )'s.