Quick 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.

Find the Minimum Word Transformation Steps

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem Transform a start word into an end word by changing exactly one character at a time. Every intermediate word and the end word must belong to the supplied dictionary. Return the minimum number of character changes. ## Function Contract Implement `minimum_word_steps(start_word, end_word, words)` and return an integer. ## Rules - All words have the same length and matching is case-sensitive. - The start word does not need to appear in the dictionary. - Return `0` when the start and end words are equal. - Return `-1` when no valid transformation exists. ## Constraints - `1 <= len(start_word) <= 20`. - `0 <= len(words) <= 100000`. - Words contain lowercase English letters and are unique. ## Examples ```text start_word = "hit" end_word = "cog" words = ["hot", "dot", "dog", "lot", "log", "cog"] output = 4 ``` One shortest sequence is `hit -> hot -> dot -> dog -> cog`.

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.

Transform start_word into end_word by changing exactly one character per step. Every intermediate word and the end word must belong to the supplied dictionary; the start word need not. All words have the same length, matching is case-sensitive, and dictionary words are unique lowercase strings. Return the minimum number of character changes, return 0 when start and end are equal, and return -1 when no valid transformation 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

  1. A breadth-first search gives the minimum number of one-character changes.
  2. Generate neighbors one position at a time and mark dictionary words visited when enqueued.

Loading coding console...

Show the approach

Approach

Treat dictionary words as vertices connected when they differ in exactly one position. Run breadth-first search from the start word without needing to insert it into the dictionary. Generate neighbors by replacing each character with each lowercase letter and accept generated words still present in an unvisited dictionary set. Removing a word when it is enqueued prevents repeats. Breadth-first search visits transformations in nondecreasing change count, so the first discovery of the end word is minimum. If the end is absent initially or never discovered, return -1.

Time complexity:
O(V L 26) expected time, where V is the number of visited words and L is word length.
Space complexity:
O(N L) for the dictionary set and breadth-first queue.