|
| 1 | +// Title: Word Ladder |
| 2 | +// Description: |
| 3 | +// A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s[1] -> s[2] -> ... -> s[k] such that: |
| 4 | +// - Every adjacent pair of words differs by a single letter. |
| 5 | +// - Every s[i] for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList. |
| 6 | +// - s[k] == endWord |
| 7 | +// 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. |
| 8 | +// Link: https://leetcode.com/problems/word-ladder/ |
| 9 | + |
| 10 | +// Time complexity: O(m*n^2) |
| 11 | +// Space complexity: O(m*n) |
| 12 | +// Variables: |
| 13 | +// m = size of the wordList |
| 14 | +// n = length of any word |
| 15 | +class Solution { |
| 16 | +public: |
| 17 | + int ladderLength(std::string beginWord, std::string endWord, std::vector<std::string> &wordList) { |
| 18 | + // put all words in the dictionary into a word set to lookup faster |
| 19 | + std::unordered_set<std::string> wordSet(wordList.begin(), wordList.end()); |
| 20 | + |
| 21 | + // start breadth-first search (BFS) with the begin word and an end-of-level mark ("") |
| 22 | + std::queue<std::string> wordQueue; { |
| 23 | + wordQueue.push(beginWord); |
| 24 | + wordQueue.push(""); |
| 25 | + } |
| 26 | + |
| 27 | + // record the current searching depth |
| 28 | + std::size_t currentDepth = 1; |
| 29 | + |
| 30 | + while (true) { |
| 31 | + // take out the next word to process |
| 32 | + std::string currentWord = wordQueue.front(); wordQueue.pop(); |
| 33 | + |
| 34 | + // end if an end-of-level mark is reached before a word |
| 35 | + if (currentWord == "") break; |
| 36 | + |
| 37 | + // return the number of words if we reached the end word |
| 38 | + if (currentWord == endWord) return currentDepth; |
| 39 | + |
| 40 | + std::string adjacentWord = currentWord; |
| 41 | + |
| 42 | + // for each position in the current word |
| 43 | + for (std::size_t i = 0; i != currentWord.size(); ++i) { |
| 44 | + // for each letter in the alphabet ... |
| 45 | + for (char c = 'a'; c <= 'z'; ++c) { |
| 46 | + // ... that is not the same as the letter at the current position |
| 47 | + if (c == currentWord[i]) continue; |
| 48 | + |
| 49 | + // replace the letter at the position with a differernt letter to make an adjacent word |
| 50 | + adjacentWord[i] = c; |
| 51 | + |
| 52 | + // if the adjacent word is in the word set |
| 53 | + if (wordSet.count(adjacentWord) != 0) { |
| 54 | + // push the adjacent word into the queue |
| 55 | + wordQueue.push(adjacentWord); |
| 56 | + |
| 57 | + // remove the word from the word set so we won't go back to the word |
| 58 | + wordSet.erase(adjacentWord); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // restore the letter at the position with the original letter |
| 63 | + adjacentWord[i] = currentWord[i]; |
| 64 | + } |
| 65 | + |
| 66 | + // check if this node is at the end of the current level |
| 67 | + if (wordQueue.front() == "") { |
| 68 | + // remove the end-of-level mark |
| 69 | + wordQueue.pop(); |
| 70 | + |
| 71 | + // append an end-of-level mark to mark the end of the next level |
| 72 | + wordQueue.push(""); |
| 73 | + |
| 74 | + // next level has one more word in the sequence |
| 75 | + currentDepth += 1; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // the end word cannot be reached with the words in the dictionary |
| 80 | + return 0; |
| 81 | + } |
| 82 | +}; |
0 commit comments