码迷,mamicode.com
首页 > 其他好文 > 详细

Word Ladder II

时间:2014-09-11 13:56:42      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:leetcode   algorithm   算法   

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.

答案

public class Solution
{
    List<List<String>> results;

    Map<String, List<String>> parents;

    public List<List<String>> findLadders(String start, String end, Set<String> dict)
    {
        results = new LinkedList<List<String>>();
        if (dict == null )
        {
            return results;
        }
        Map<String, Integer> ladder = new HashMap<String, Integer>();
        int minLEN = Integer.MAX_VALUE;
        LinkedList<String> queue = new LinkedList<String>();
        dict.add(end);
        for (String word : dict)
        {
            ladder.put(word, minLEN);
        }
        parents = new HashMap<String, List<String>>(dict.size() * 2);
        queue.add(start);
        ladder.put(start, 1);
        parents.put(start, new LinkedList<String>());
        for (String word = queue.poll(); word != null; word = queue.poll())
        {
            int length = ladder.get(word) + 1;
            if (length > minLEN)
            {
                break;
            }
            for (int i = 0; i < word.length(); i++ )
            {
                StringBuilder newBuilder = new StringBuilder(word);
                for (char c = 'a'; c <= 'z'; c++ )
                {
                    newBuilder.setCharAt(i, c);
                    String newWord = newBuilder.toString();
                    if (ladder.containsKey(newWord))
                    {
                        if (length > ladder.get(newWord))
                        {
                            continue;
                        }
                        if (length < ladder.get(newWord))
                        {
                            ladder.put(newWord, length);
                            List<String> list = new LinkedList<String>();
                            list.add(word);
                            parents.put(newWord, list);
                            queue.add(newWord);
                        }
                        else
                        {
                            parents.get(newWord).add(word);
                        }
                        if (newWord.equals(end))
                        {
                            minLEN = length;
                        }
                    }
                }
            }
        }
        if (minLEN < Integer.MAX_VALUE)
        {
            backToStart(start, end, new LinkedList<String>());
        }
        return results;
    }
    void backToStart(String start, String end, List<String> list)
    {
        if (start.equals(end))
        {
            list.add(0, start);
            results.add(new ArrayList<String>(list));
            list.remove(0);
            return;
        }
        list.add(0,end);
        for (String element : parents.get(end))
        {
            backToStart(start, element, list);
        }
        list.remove(0);
    }
}


Word Ladder II

标签:leetcode   algorithm   算法   

原文地址:http://blog.csdn.net/jiewuyou/article/details/39205069

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!