您的位置:首页 > 其它

【LeetCode】Word Ladder

2013-12-31 14:21 471 查看
Word Ladder

Total Accepted: 3388 Total Submissions: 21998 My Submissions

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

Only one letter can be changed at a time

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.
好吧,我承认我看了很久才明白这题是什么意思。

描述一下,从start开始,每次变化一个字母,变化后的字母必须在dict中,问最短需要几步可以走到end。

BFS吧。

测试用例有bug,题目给出的例子没有当做test case。

给出的用例应该是end都在dict中。

所以以下的代码也可以AC,其实是有问题的。

public class Solution {
    public static int ladderLength(String start, String end,HashSet<String> dict) {
		if (dict == null || dict.isEmpty()) {
			return 0;
		}
		Queue<Node> strQueue = new LinkedList<Node>();
		HashSet<String> visitedSet = new HashSet<String>();
		strQueue.add(new Node(start, 0));
		visitedSet.add(start);
		int wordLen = start.length();
		while (!strQueue.isEmpty()) {
			Node node = strQueue.peek();
			strQueue.poll();
			for (int i = 0; i < wordLen; i++) {
			    StringBuffer sb = new StringBuffer(node.word);
				for (char j = 'a'; j <= 'z'; j++) {
					sb.setCharAt(i, j);
					String tempWord = sb.toString();
					if (dict.contains(tempWord) && !visitedSet.contains(tempWord)) {
						if (tempWord.equals(end)) {
							return node.len+2;
						}else {
							strQueue.add(new Node(tempWord, node.len+1));
							visitedSet.add(tempWord);
						}
					}
				}
			}
		}
		return 0;
	}
	private static class Node{
		String word;
		int len;
		public Node(String word, int len) {
			super();
			this.word = word;
			this.len = len;
		}
	}
}

Java AC

public class Solution {
    public int ladderLength(String start, String end,HashSet<String> dict) {
		if (dict == null || dict.isEmpty()) {
			return 0;
		}
		Queue<Node> strQueue = new LinkedList<Node>();
		HashSet<String> visitedSet = new HashSet<String>();
		strQueue.add(new Node(start, 1));
		visitedSet.add(start);
		int wordLen = start.length();
		while (!strQueue.isEmpty()) {
			Node node = strQueue.peek();
			strQueue.poll();
			for (int i = 0; i < wordLen; i++) {
			    StringBuffer sb = new StringBuffer(node.word);
				for (char j = 'a'; j <= 'z'; j++) {
					sb.setCharAt(i, j);
					String tempWord = sb.toString();
					if (tempWord.equals(end)) {
						return node.len+1;
					}else {
						if (dict.contains(tempWord) && !visitedSet.contains(tempWord)) {
							strQueue.add(new Node(tempWord, node.len+1));
							visitedSet.add(tempWord);
						}
					}
				}
			}
		}
		return 0;
	}
	private static class Node{
		String word;
		int len;
		public Node(String word, int len) {
			super();
			this.word = word;
			this.len = len;
		}
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: