• [LeetCode] Word Ladder


    Well, this problem has a nice BFS structure.

    Let's see the example in the problem statement.

    start = "hit"

    end = "cog"

    dict = ["hot", "dot", "dog", "lot", "log"]

    Since only one letter can be changed at a time, if we start from "hit", we can only change to those words which have only one different letter from it, like "hot". Putting in graph-theoretic terms, we can say that "hot" is a neighbor of "hit".

    The idea is simpy to begin from start, then visit its neighbors, then the non-visited neighbors of its neighbors... Well, this is just the typical BFS structure.

    To simplify the problem, we insert end into dict. Once we meet end during the BFS, we know we have found the answer. We maintain a variable ladder for the current distance of the transformation and update it by ladder++ once we finish a round of BFS search (note that it should fit the definition of the distance in the problem statement). Also, to avoid visiting a word for more than once, we erase it from dict once it is visited.

    The code is as follows.

     1 class Solution {
     2 public:
     3     int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
     4         wordDict.erase(beginWord);
     5         wordDict.insert(endWord);
     6         int ladder = 2, len = beginWord.length();
     7         queue<string> nextWords;
     8         nextWords.push(beginWord);
     9         while (!nextWords.empty()) {
    10             int num = nextWords.size();
    11             for (int i = 0; i < num; i++) {
    12                 string word = nextWords.front();
    13                 nextWords.pop();
    14                 for (int p = 0; p < len; p++) {
    15                     char letter = word[p];
    16                     for (int j = 0; j < 26; j++) {
    17                         word[p] = 'a' + j;
    18                         if (wordDict.find(word) != wordDict.end()) {
    19                             if (word == endWord) return ladder;
    20                             wordDict.erase(word);
    21                             nextWords.push(word);
    22                         }
    23                     }
    24                     word[p] = letter;
    25                 }
    26             }
    27             ladder++;
    28         }
    29         return 0;
    30     }
    31 };

    The above code can still be speed up if we also begin from end. Once we meet the same word from start and end, we know we are done. This link provides a nice two-end search solution. I rewrite the code below for better readability. At each round of BFS, depending on the relative size of nextWords and prevWords, we swap the smaller one to the working setto reduce the running time (lines 12 - 13). 

     1 class Solution {
     2 public:
     3     int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
     4         wordDict.erase(beginWord);
     5         wordDict.erase(endWord);
     6         unordered_set<string> nextWords;
     7         unordered_set<string> prevWords;
     8         nextWords.insert(beginWord);
     9         prevWords.insert(endWord);
    10         int ladder = 2, len = beginWord.length();
    11         while (!nextWords.empty() && !prevWords.empty()) {
    12             if (nextWords.size() > prevWords.size())
    13                 swap(nextWords, prevWords);
    14             unordered_set<string>::iterator itr = nextWords.begin();
    15             unordered_set<string> temp;
    16             for (; itr != nextWords.end(); itr++) {
    17                 string word = *itr;
    18                 for (int p = 0; p < len; p++) {
    19                     char letter = word[p];
    20                     for (int j = 0; j < 26; j++) {
    21                         word[p] = 'a' + j;
    22                         if (prevWords.find(word) != prevWords.end())
    23                             return ladder;
    24                         if (wordDict.find(word) != wordDict.end()) {
    25                             temp.insert(word);
    26                             wordDict.erase(word);
    27                         }
    28                     }
    29                     word[p] = letter;
    30                 }
    31             }
    32             swap(nextWords, temp);
    33             ladder++;
    34         }
    35         return 0;
    36     }
    37 };
  • 相关阅读:
    springmvc中request的线程安全问题
    SpringMVC数据验证
    Spring验证的错误返回------BindingResult
    chrome扩展
    北京联想招聘-Android高级工程师(5-7年) 加入qq 群:220486180 或者直接在此 留言咨询
    北京联想招聘-Android Framework高级工程师(7-10年) 加入qq 群:220486180 或者直接在此 留言咨询
    北京联想招聘-java 云服务开发工程师 加入qq 群:220486180 或者直接在此 留言咨询
    北京联想招聘-IOS高级 加入qq 群:220486180 或者直接在此 留言咨询
    css 兼容
    appium 几点总结(转)
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4599041.html
Copyright © 2020-2023  润新知