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:

  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.

我借用了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;
    }
}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 遇到职业搞坏淘宝店铺的买家怎么办 眼破裂伤无光感半个月怎么办 出了虫的豆豆熬稀饭喝了怎么办? 果汁阳台月季叶子掉光了怎么办 近看好看远看难看该怎么办 衣服褶皱没有熨斗的情况下怎么办 裤子磨得发亮怎么办也没有电熨斗 老是在灯箱拍照对眼睛不好怎么办 电信光纤宽带账号密码忘记了怎么办 遇到尴尬的事情自己缓不过来怎么办 注销微信账号显示非法请求怎么办 微信备份以前的被覆盖了怎么办 之前微信号被新微信号覆盖了怎么办 微信发出的消息变成绿色怎么办 收了客户的资金被骗走了怎么办 淘宝退回去的衣服店家不接收怎么办 淘宝同款衣服价格相差很大该怎么办 淘宝买的衣服退回去了不退钱怎么办 淘宝客人退回的衣服有口红印怎么办 淘宝拍产品照片被投诉著作权怎么办 员工总在节假日忙的时候请假怎么办 买东西商家少给了货应该怎么办 买家退回的衣服有污渍卖家该怎么办 商家说衣服有污渍不退怎么办 退回商家换货不给寄应怎么办 毕业照跟拍摄影师拍砸了怎么办 韵达快递寄快递快递单号丢了怎么办 韵达快递把我的户口本弄丢了怎么办 淘宝卖家发货与实际货物不符怎么办 寄出去的快递不知道物流单号怎么办 淘宝买家所需要的货物填错怎么办 淘宝卖家顾客拒绝签收货要怎么办 闲鱼买家申请退货退款不发货怎么办 在闲鱼买东西买家恶意退货怎么办 淘宝卖家给的退货地址是国外怎么办 淘宝顾客下单了一件代发怎么办 闲鱼上卖东西快递单号填错了怎么办 淘宝收货后快递显示不签收怎么办 淘宝快递显示签收但没收到货怎么办 国外客人收到货后嫌弃质量差怎么办 理发15涨到20客人少了怎么办