|
| 1 | +class Solution { |
| 2 | +public: |
| 3 | + |
| 4 | + vector<vector<string>> result; |
| 5 | + |
| 6 | + //find all paths upto min-depth using DFS |
| 7 | + void DFS(string &beginWord, string &endWord, vector<string> &path, unordered_map<string, unordered_set<string>> &adjacencyList) { |
| 8 | + |
| 9 | + path.push_back(beginWord); |
| 10 | + |
| 11 | + if(beginWord == endWord) { //terminating condition |
| 12 | + result.push_back(path); |
| 13 | + path.clear(); |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + for(auto child : adjacencyList[beginWord]) { |
| 18 | + DFS(child, endWord, path, adjacencyList); |
| 19 | + } |
| 20 | + |
| 21 | + path.pop_back(); //backtrack |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) { |
| 26 | + |
| 27 | + if(beginWord.size() == 0 or endWord.size() == 0 or wordList.size() == 0) return vector<vector<string>>(); |
| 28 | + |
| 29 | + queue<string> Q; //BFS |
| 30 | + unordered_map<string, int> visited; // Key => Node, Value => Depth |
| 31 | + unordered_map<string, unordered_set<string>> adjacencyList; //store list for immediate children to current parent (next level) |
| 32 | + unordered_set<string> words(wordList.begin(), wordList.end()); //set of words |
| 33 | + |
| 34 | + if(words.find(endWord) == words.end()) return vector<vector<string>>(); |
| 35 | + |
| 36 | + //get min-depth via BFS |
| 37 | + Q.push(beginWord); |
| 38 | + visited[beginWord] = 0; |
| 39 | + |
| 40 | + while(!Q.empty()) { |
| 41 | + |
| 42 | + auto currentWord = Q.front(); |
| 43 | + Q.pop(); |
| 44 | + |
| 45 | + string temp = currentWord; |
| 46 | + for(int i=0; i<currentWord.size(); i++) { |
| 47 | + |
| 48 | + for(char x='a'; x<='z'; x++) { |
| 49 | + |
| 50 | + if(temp[i] == x) continue; //if same character as that of original string |
| 51 | + |
| 52 | + temp[i] = x; //change original character |
| 53 | + |
| 54 | + if(words.find(temp) != words.end()) { //current string(temp) in wordList |
| 55 | + |
| 56 | + if(visited.find(temp) == visited.end()) { //current string(temp) already not visited.. means its a new child |
| 57 | + visited[temp] = visited[currentWord] + 1; //store level of current child as level_of_parent+1 |
| 58 | + Q.push(temp); //push current child to Q |
| 59 | + adjacencyList[currentWord].insert(temp); //add next level child as an adjacent node to currentWord(parent) |
| 60 | + } |
| 61 | + |
| 62 | + else if(visited[temp] == visited[currentWord] + 1) { //if the current child is already visited and its the child in the new level below level of currentWord(parent) |
| 63 | + |
| 64 | + adjacencyList[currentWord].insert(temp); //add next level child as an adjacent node to currentWord(parent) |
| 65 | + |
| 66 | + } |
| 67 | + } |
| 68 | + temp[i] = currentWord[i]; //replace original character |
| 69 | + |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + vector<string> path; |
| 76 | + DFS(beginWord, endWord, path, adjacencyList); |
| 77 | + |
| 78 | + return result; |
| 79 | + } |
| 80 | +}; |
0 commit comments