A simple algorithm for the constrained sequence problems