backbenchcode
← all problemsnext: Two-Pointer Sum
#12HardFrom Research to FAANG · 33% acceptance

Shortest Transformation

BFSGraphString

Given a start word, an end word, and a dictionary, return the length of the shortest sequence transforming start into end, changing one letter at a time, where every intermediate word is in the dictionary.

If you have ever thought about minimal mutation paths between sequences, the framing will feel familiar — it is BFS on a graph you never explicitly built.

Examples

Input: begin="hit", end="cog", words=["hot","dot","dog","lot","log","cog"]

Output: 5

Note: hit → hot → dot → dog → cog.

Constraints

Hints

solution.pyPython 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Test console

// hit Run to execute the sample tests

// Interactive editor preview. Live code execution arrives with your early-access cohort.