Trains the technique from
LeetCode 433Minimum Genetic MutationThis 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 bottling line has eight stations in a row, and each station carries one of four nozzle types, written "A", "C", "G" or "T". A fitting of the whole line is therefore a string of exactly eight of those letters, read from the first station to the last.
The line is currently on the fitting start and the shift plan calls for the fitting target. A single retool takes the line down and swaps the nozzle on exactly one station, leaving the other seven alone.
Safety rules only allow the line to run on a fitting drawn from approved. Every fitting the line comes to rest on, including target, has to be on that list. The fitting the line starts on does not have to be on the list, since it is already running. The list may be empty and may name the same fitting more than once.
Return the fewest retools that take the line from start to target, or -1 when no run of retools does.
Example 1
Swapping station 8, then station 7, then station 6 gets there, and each of the three fittings the line rests on is on the approved list.
Example 2
Each approved fitting here differs from the current one on two stations, and a retool touches one station, so the line has nowhere legal to rest.
Example 3
The line already carries the fitting the shift plan asks for, so no station is touched.
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 fewest_retools(start: str, target: str, approved: list[str]) -> int:public int fewestRetools(String start, String target, String[] approved)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.