A small algorithm for substring elimination