Find the Minimum Word Transformation Steps
Company: Salesforce
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Compute the minimum number of one-character transformations between equal-length words when every intermediate word must belong to a supplied dictionary, returning failure when no path exists.
Constraints
- 1 <= len(start_word) = len(end_word) <= 20.
- 0 <= len(words) <= 100000.
- All dictionary words have the same length, contain lowercase English letters, and are unique.
- The start word need not appear in the dictionary, but every intermediate word and the end word must.
- Return 0 for equal endpoints and -1 when no transformation exists.
Examples
Input: ('hit', 'cog', ['hot', 'dot', 'dog', 'lot', 'log', 'cog'])
Expected Output: 4
Explanation: This is the source example; one shortest sequence changes four characters across four steps.
Input: ('same', 'same', [])
Expected Output: 0
Explanation: Equal start and end words require no changes even with an empty dictionary.
Hints
- A breadth-first search gives the minimum number of one-character changes.
- Generate neighbors one position at a time and mark dictionary words visited when enqueued.