Advertisement
nikunjsoni

126

May 4th, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
  4.         unordered_map<string, int> m;
  5.         unordered_map<int, string> revm;
  6.         int count = 1;
  7.         for(string word: wordList){
  8.             m[word] = count++;
  9.             revm[count-1] = word;
  10.         }
  11.         if(!m.count(beginWord)){
  12.             m[beginWord] = count++;
  13.             revm[count-1] = beginWord;
  14.         }
  15.        
  16.         // Create graph.
  17.         vector<vector<int>> graph(count);
  18.         for(auto p: m){
  19.             string word = p.first;
  20.             int wordIdx = p.second;
  21.             for(int i=0; i<word.length(); i++){
  22.                 for(int j=1; j<26; j++){
  23.                     string nextWord = word;
  24.                     nextWord[i] = (nextWord[i]-'a'+j)%26 + 'a';
  25.                     if(m.count(nextWord)){
  26.                         graph[wordIdx].push_back(m[nextWord]);
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.        
  32.         // Do BFS.
  33.         vector<vector<int>> idx;
  34.         if(!m.count(endWord)) return vector<vector<string>>();
  35.         queue<pair<int, vector<int>>> q;
  36.         int source = m[beginWord];
  37.         vector<int> seq = {source};
  38.         vector<bool> vis(count, false);
  39.         q.push({source, seq});
  40.         int dest = m[endWord];
  41.        
  42.         while(!q.empty()){
  43.             int sz = q.size();
  44.             unordered_set<int> curVis;
  45.             for(int i=0; i<sz; i++){
  46.                 auto p = q.front(); q.pop();
  47.                 int curr = p.first;
  48.                 for(int child: graph[curr]){
  49.                     if(vis[child]) continue;
  50.                     vector<int> nextSeq(p.second);
  51.                     nextSeq.push_back(child);
  52.                     if(child == dest){
  53.                         idx.push_back(nextSeq);
  54.                         break;
  55.                     }
  56.                     q.push({child, nextSeq});
  57.                     curVis.insert(child);
  58.                 }
  59.             }
  60.             if(idx.size()) break;
  61.             for(auto it=curVis.begin(); it != curVis.end(); it++){
  62.                 vis[*it] = true;
  63.             }
  64.         }
  65.         vector<vector<string>> ans;
  66.         for(auto vec: idx){
  67.             vector<string> path;
  68.             for(auto i: vec)
  69.                 path.push_back(revm[i]);
  70.             ans.push_back(path);
  71.         }
  72.         return ans;
  73.     }
  74. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement