|
| 1 | +[](https://github.com/LeetCode-in-Net/LeetCode-in-Net) |
| 2 | +[](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork) |
| 3 | + |
| 4 | +## 127\. Word Ladder |
| 5 | + |
| 6 | +Hard |
| 7 | + |
| 8 | +A **transformation sequence** from word `beginWord` to word `endWord` using a dictionary `wordList` is a sequence of words <code>beginWord -> s<sub>1</sub> -> s<sub>2</sub> -> ... -> s<sub>k</sub></code> such that: |
| 9 | + |
| 10 | +* Every adjacent pair of words differs by a single letter. |
| 11 | +* Every <code>s<sub>i</sub></code> for `1 <= i <= k` is in `wordList`. Note that `beginWord` does not need to be in `wordList`. |
| 12 | +* <code>s<sub>k</sub> == endWord</code> |
| 13 | + |
| 14 | +Given two words, `beginWord` and `endWord`, and a dictionary `wordList`, return _the **number of words** in the **shortest transformation sequence** from_ `beginWord` _to_ `endWord`_, or_ `0` _if no such sequence exists._ |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +**Input:** beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] |
| 19 | + |
| 20 | +**Output:** 5 |
| 21 | + |
| 22 | +**Explanation:** One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long. |
| 23 | + |
| 24 | +**Example 2:** |
| 25 | + |
| 26 | +**Input:** beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"] |
| 27 | + |
| 28 | +**Output:** 0 |
| 29 | + |
| 30 | +**Explanation:** The endWord "cog" is not in wordList, therefore there is no valid transformation sequence. |
| 31 | + |
| 32 | +**Constraints:** |
| 33 | + |
| 34 | +* `1 <= beginWord.length <= 10` |
| 35 | +* `endWord.length == beginWord.length` |
| 36 | +* `1 <= wordList.length <= 5000` |
| 37 | +* `wordList[i].length == beginWord.length` |
| 38 | +* `beginWord`, `endWord`, and `wordList[i]` consist of lowercase English letters. |
| 39 | +* `beginWord != endWord` |
| 40 | +* All the words in `wordList` are **unique**. |
| 41 | + |
| 42 | +## Solution |
| 43 | + |
| 44 | +```csharp |
| 45 | +using System.Collections.Generic; |
| 46 | + |
| 47 | +public class Solution { |
| 48 | + public int LadderLength(string beginWord, string endWord, IList<string> wordDict) { |
| 49 | + var beginSet = new HashSet<string>(); |
| 50 | + var endSet = new HashSet<string>(); |
| 51 | + var wordSet = new HashSet<string>(wordDict); |
| 52 | + var visited = new HashSet<string>(); |
| 53 | + if (!wordDict.Contains(endWord)) { |
| 54 | + return 0; |
| 55 | + } |
| 56 | + int len = 1; |
| 57 | + int strLen = beginWord.Length; |
| 58 | + beginSet.Add(beginWord); |
| 59 | + endSet.Add(endWord); |
| 60 | + while (beginSet.Count > 0 && endSet.Count > 0) { |
| 61 | + if (beginSet.Count > endSet.Count) { |
| 62 | + var temp = beginSet; |
| 63 | + beginSet = endSet; |
| 64 | + endSet = temp; |
| 65 | + } |
| 66 | + var tempSet = new HashSet<string>(); |
| 67 | + foreach (var s in beginSet) { |
| 68 | + char[] chars = s.ToCharArray(); |
| 69 | + for (int i = 0; i < strLen; i++) { |
| 70 | + char old = chars[i]; |
| 71 | + for (char j = 'a'; j <= 'z'; j++) { |
| 72 | + chars[i] = j; |
| 73 | + string temp = new string(chars); |
| 74 | + if (endSet.Contains(temp)) { |
| 75 | + return len + 1; |
| 76 | + } |
| 77 | + if (!visited.Contains(temp) && wordSet.Contains(temp)) { |
| 78 | + tempSet.Add(temp); |
| 79 | + visited.Add(temp); |
| 80 | + } |
| 81 | + } |
| 82 | + chars[i] = old; |
| 83 | + } |
| 84 | + } |
| 85 | + beginSet = tempSet; |
| 86 | + len++; |
| 87 | + } |
| 88 | + return 0; |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
0 commit comments