Word Ladder I

Given two distinct lowercase words startWord and targetWord, and a list wordList of unique words of the same length, return the length of the shortest legal…

Given two distinct lowercase words `startWord` and `targetWord`, and a list `wordList` of unique words of the same length, return the length of the shortest legal transformation sequence from the start to the target. Each transformation changes exactly one letter. Every transformed word must be in `wordList`, including the target. The start itself need not be in the list. Count words in the sequence, including both endpoints, not transformations. Return `0` if no legal sequence exists. The function must not modify `wordList`.

- `N` is the number of words in `wordList`; `1 <= N <= 100`. - `M` is the common word length; `1 <= M <= 10`. - All words contain lowercase English letters only. - Dictionary words are unique and all lengths equal both endpoint lengths. - `startWord != targetWord`; the start may be absent from the dictionary. - A missing target is valid input and has answer `0`.

Example 1: input startWord = "der", targetWord = "dfs", wordList = ["des","der","dfr","dgt","dfs"]; output 3. der -> dfr -> dfs contains three words and two transformations.

Example 2: input startWord = "gedk", targetWord = "geek", wordList = ["geek","gefk"]; output 2. The start is not in the list, but gedk -> geek is legal and contains two words.

Example 3: input startWord = "a", targetWord = "b", wordList = ["c"]; output 0. The target is absent. Replacing a letter with a nondictionary target is not legal.

Category: Graphs. Topic: graph. Difficulty: hard. Patterns: graph-traversal.