Trains the technique from
LeetCode 488Zuma GameThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A rail holds beads, given by rail, and a pouch holds spares, given by pouch. Both are written with the letters R, Y, B, G and W, one letter per bead.
One move takes a bead out of the pouch and pushes it in anywhere on the rail, either end included. Then, over and over for as long as it applies, any run of three or more touching beads of one colour is taken off the rail, which may bring fresh runs together.
The rail starts with no run of three or more beads of one colour.
Return the fewest beads that have to leave the pouch to clear the rail completely, or -1 when no number of moves can clear it.
Example 1
Pushing the spare between the two middle beads makes three of that colour. Taking those off brings the four outer beads together, and they come off as well, so one bead clears the rail.
Example 2
Push the first spare between the two middle beads to make three, which come off and leave the outer pair touching. The second spare then makes three of those.
Example 3
Each colour appears once on the rail and once in the pouch, so no colour can ever reach three touching beads.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def find_min_step(rail: str, pouch: str) -> int:public int findMinStep(String rail, String pouch)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.