Word Ladder
来源:互联网 发布:dns劫持后的域名来路 编辑:程序博客网 时间:2024/04/25 15:42
Problem:
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.
我借用了dijkstra算法的思想。与dijkstra算法不同的是,每次对邻接点进行松弛后不用找最小值,因为步长只会是1,所有能够到达的节点都已是最短路径。如果字典的容量是n,单词的长度是m,算法的时间复杂度为O(26*n*m)。
Solution:
public class Solution {
HashSet<String> dic;
public int ladderLength(String start, String end, Set<String> dict) {
if(dict==null||start==null||end==null||dict.size()==0||start.equals("")||end.equals(""))
return 0;
if(start.equals(end))
return 1;
dic = new HashSet<>(dict);
dic.add(end);
int pathLen = 1;
LinkedList<String> curStrings = new LinkedList<>();
curStrings.add(start);
while(!curStrings.isEmpty())
{
int count = curStrings.size();
pathLen++;
while(count-->0)
{
String str = curStrings.poll();
char[] chars = str.toCharArray();
for(int i=0;i<chars.length;i++)
{
char temp = chars[i];
chars[i] = 'a'-1;
while(chars[i]++<'z')
{
if(chars[i]==temp)
continue;
String s = new String(chars);
if(s.equals(end))
return pathLen;
if(dic.contains(s))
{
dic.remove(s);
curStrings.offer(s);
}
}
chars[i] = temp;
}
}
}
return 0;
}
}
HashSet<String> dic;
public int ladderLength(String start, String end, Set<String> dict) {
if(dict==null||start==null||end==null||dict.size()==0||start.equals("")||end.equals(""))
return 0;
if(start.equals(end))
return 1;
dic = new HashSet<>(dict);
dic.add(end);
int pathLen = 1;
LinkedList<String> curStrings = new LinkedList<>();
curStrings.add(start);
while(!curStrings.isEmpty())
{
int count = curStrings.size();
pathLen++;
while(count-->0)
{
String str = curStrings.poll();
char[] chars = str.toCharArray();
for(int i=0;i<chars.length;i++)
{
char temp = chars[i];
chars[i] = 'a'-1;
while(chars[i]++<'z')
{
if(chars[i]==temp)
continue;
String s = new String(chars);
if(s.equals(end))
return pathLen;
if(dic.contains(s))
{
dic.remove(s);
curStrings.offer(s);
}
}
chars[i] = temp;
}
}
}
return 0;
}
}
0 0
- Word Ladder
- Word Ladder
- word ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- Word Ladder
- 【异或】SOJ Find Differences
- Sum Root to Leaf Numbers
- day38,page60,total390
- Longest Consecutive Sequence
- ubuntu 下eclipse 打不开
- Word Ladder
- 各种工业以太网比较(EtherCAT,EtherNet/IP,ProfiNet,Modbus-TCP,Powerlink)
- VMware上新建windows 10 虚拟机(1)
- Binary Tree Maximum Path Sum
- Best Time to Buy and Sell Stock
- 在cocos2dx中的点击事件
- Python模块学习 之zipfile zip文件操作
- 如何实现广度优先遍历(BFS)
- 求n个数的子集的三种思路