Word Ladder

来源:互联网 发布:easyui 表单插件js 编辑:程序博客网 时间:2024/05/18 05:32

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence 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"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

public class Solution {    public int ladderLength(String start, String end, Set<String> dict) {        if(start.length()!=end.length()) return 0;        if(start.length()<=0 || end.length()<=0) return 0;        LinkedList<String> action=new LinkedList<String>();        LinkedList<String> next=new LinkedList<String>();        Set<String> visited=new HashSet<String>();        Map<String,String> father=new TreeMap<String,String>();        boolean found=false;        int steps=0;        action.add(start);        while(!action.isEmpty() && !found){            steps++;            while(!action.isEmpty() && !found){                String w=action.poll();                for(int i=0;i<w.length();i++){                    char[] wordArray=w.toCharArray();                    for(char c='a';c<='z';c++){                        if(c==w.charAt(i)) continue;                        char tmp=wordArray[i];                        wordArray[i]=c;                        String v=new String(wordArray);                        if(v.equals(end)){                            found=true;                            father.put(v,w);                            break;                        }                        if(dict.contains(w) && !visited.contains(v)){                            next.add(v);                            visited.add(v);                            father.put(v,w);                        }                        wordArray[i]=tmp;                    }                }            }            action=next;            next=new LinkedList<String>();        }        if(found) return steps+1;        else return 0;    }}
思路:java代码超时,同样思路的C++代码通过,不过也跑了1000ms。

class Solution {public:    int ladderLength(string start, string end, unordered_set<string> &dict) {        if(start.size()!=end.size()) return 0;        if(start.empty() || end.empty()) return 0;                queue<string> next,current;        set<string> visited;        map<string,string> father;        int level=0;        bool found=false;                current.push(start);        while(!current.empty() && !found){            level++;            while(!current.empty() && !found){                string str(current.front()); current.pop();                for(int i=0; i<str.size();i++){                    string new_word(str);                    for(char c='a';c<='z';c++){                        if(c==new_word[i]) continue;                        swap(c,new_word[i]);                        if(new_word==end){                            found=true;                            father[new_word]=str;                            break;                        }                        if(dict.count(new_word)>0 && !visited.count(new_word)){                            next.push(new_word);                            visited.insert(new_word);                            father[new_word]=str;                        }                        swap(c,new_word[i]);                    }                }            }            swap(next,current);        }        if(found) return level+1;        else return 0;    }};


0 0