leetcode 126: Word Ladder ll (uncompleted)

Word Ladder II Feb 11

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

Return

  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
class Solution {
public:
    vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        vector<vector<string> > res;
        if(start.size()!=end.size()) return res;
       // if(start == end) return ;
        
        unordered_set<string> unique;
        unique.insert(start);
        
        queue<string> que;
        unordered_map<string, string> path;
        
        que.push(start);
        path[start] = "";
        int q1=1;
        int q2=0;
        
        while(q1>0) {
            string s = que.front();
            que.pop();
            --q1;
            string pre = "";
            for(int i=0; i<s.size(); i++) {
                string temp = s;
                for(char c='a'; c<='z'; c++) {
                    temp[i] = c;
                    if(dict.find(temp)!=dict.end() && unique.find(temp)==dict.end()) {
                        if(temp == end) {
                            if(pre!="") {
                                unique.erase(pre);
                            }
                            pre = temp;
                            vector<string> elems;
                            elems.push_back(end);
                            string item = s;
                            while(item!="") {
                                elems.insert(elems.begin(), item);
                                item = path[item];
                            }
                            res.push_back(elems);
                        } else {
                            unique.insert(temp);
                            que.push(temp);
                            path[temp] = s;
                            ++q2;
                        }
                    }
                }
            }
            
            if(q1==0) {
                if(!res.empty()) return res;
                q1 = q2;
                q2 = 0;
            }
        }
        return res;
    }
};

class Solution {
public:
     class TreeNode{
        public:
            string val;
            TreeNode * parent;
            TreeNode(string str, TreeNode* p=NULL):val(str), parent(p){}  
        };


    bool findnode(string temp,  TreeNode* p) {
        while(p!=NULL) {
            if( temp == p->val) return false;
            p = p->parent;
        }
        return true;
    }


    vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<TreeNode*> clean;
        vector<vector<string> > res;
        if(start.size()!=end.size()) return res;
        
       // if(start == end) return ;
        queue<TreeNode*> que;
        TreeNode *head = new TreeNode(start);
        clean.push_back(head);
        TreeNode *p = head;
        que.push(p);
        int q1=1;
        int q2=0;
        
        while(q1>0) {
            TreeNode * p = que.front();
            string s = p->val;
            que.pop();
            --q1;
            
            for(int i=0; i<s.size(); i++) {
                string temp = s;
                for(char c='a'; c<='z'; c++) {
                    temp[i] = c;
                    if(dict.find(temp)!=dict.end() && findnode(temp, p) ) {
                        if(temp == end) {
                            vector<string> elems;
                            elems.push_back(end);
                            while(p!=NULL) {
                                elems.insert(elems.begin(), p->val);
                                p = p->parent;
                            }
                            res.push_back(elems);
                        } else {
                            TreeNode* tn = new TreeNode(temp,p);
                            clean.push_back(tn);
                            que.push(tn);
                            ++q2;
                        }
                    }
                }
            }
            
            if(q1==0) {
                if(!res.empty()) return res;
                q1 = q2;
                q2 = 0;
            }
        }
        
        for(int i=0; i<clean.size(); i++) {
            delete clean[i];
            clean[i] = NULL;
        }
        return res;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值