AdobeOA #5
Determine the Edit Distance in a Word Ladder
Estimated HardJudge-ready
Topics (AI-labelled)Shortest PathsHashing
Each operation replaces one character, and every word passed through must be in the word list (the start word need not be). If the target word is not in the word list there is no solution, but when the start and target are identical no operation is needed. Find the minimum number of replacements.
Input
The input is a JSON array [begin,end,wordList]. The original gives no bounds on length, characters or count; this site sets each word to 1..20 Unicode scalars and the word list to 0..10000 entries, and the start and target words may differ in length. All code points total ≤200040, and standard JSON preserves whitespace/NUL. Words of different lengths cannot reach each other by replacement; if there is no solution, output −1.
Output
Output a single integer answer.
Example 1
Input:
["hit","cog",["hit","hot","dot","dog","cog"]]
Output:
4
Example 2
Input:
["word","word",["word","ward"]]
Output:
0
Example 3
Input:
["hit","cog",["hit","hot","dot","dog"]]
Output:
-1
Example notes
The outputs of the three public examples are, in order: 4; 0; -1. Verified by independent enumeration or direct simulation.
Editorial
Loading solution…