Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time.
  2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.

Example 1:

Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"] Output: 5 Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

思路

BFS

代码

 class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
// use dict to check duplicats
Set<String> dict = new HashSet<>(wordList);
Queue<String> queue = new LinkedList<>();
queue.add(beginWord);
int level = 0;
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size; i++){
String cur = queue.remove();
if(cur.equals(endWord)){ return level + 1;}
for(int j = 0; j < cur.length(); j++){
// hit -> {'h', 'i', 't'}
char[] charArray = cur.toCharArray();
for(char c = 'a'; c <='z'; c++){
// {'h', 'i', 't'} for'h', try checking 'a','b'...'z' which forms ait, bit...zit
charArray[j] = c;
String temp = new String(charArray);
if(dict.contains(temp)){
queue.add(temp);
// to avoid dead loop, like hit will find hit itself
dict.remove(temp);
}
}
}
}
level++;
}
return 0;
}
}

[leetcode]127. Word Ladder单词接龙的更多相关文章

  1. LeetCode 127. Word Ladder 单词接龙(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

  2. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  3. 127 Word Ladder 单词接龙

    给出两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列,转换需遵循如下规则:    每次只能改变一个字母.    变换过程中的 ...

  4. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  5. Leetcode#127 Word Ladder

    原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...

  6. leetcode@ [127] Word Ladder (BFS / Graph)

    https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...

  7. leetcode 127. Word Ladder ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  8. [LeetCode] 127. Word Ladder _Medium tag: BFS

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  9. Java for LeetCode 127 Word Ladder

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

随机推荐

  1. Hydra密码破译工具

    Hydra简介 Hydra是著名黑客组织thc开发的一款开源的暴力密码破解工具,可以在线破解多种密码,目前已经被Backtrack和kali等渗透平台收录.除了命令行下的Hydra外,还提供了Hydr ...

  2. python实现根据指定字符截取对应的行的内容

    工作中遇到的,在一个.c文件中有很多函数,这个.c是自动生成的,需要将所有的函数通过extern放到.h中,每个函数都是UINT32 O_开头,通过正则表达式进行字符匹配以及通过linecache来截 ...

  3. 第七次Scrum冲刺

    第七次Scrum冲刺 1.今日完成的任务 队员 今日完成任务 刘佳 前端与后端对接 李佳 后端与数据库对接 周世元 数据库与后端对接 杨小妮 博客编写 许燕婷 管理团队当日及次日任务 陈水莲 综合测试 ...

  4. c# vs2013部署项目

    http://demo.netfoucs.com/shinepan/article/details/24865931

  5. javascript:getElementsByName td name

    问题:    今天写动态生成HTML表格的时候需要用到统计td内的数据,在生成的时候设置了td的name属性,但是document.getElementsByName("tdname&quo ...

  6. spring boot 集成Thymeleaf

                                           

  7. Oracle 连接数据库

    使用的DLL:Oracle.ManagedDataAccess Bug:OracleInternal.Common.ProviderConfig的类型初始值设定项引发异常 App.config的更改才 ...

  8. 吴裕雄 python深度学习与实践(6)

    from pylab import * import pandas as pd import matplotlib.pyplot as plot import numpy as np filePath ...

  9. ivew 表格中的input数据改变就会失去焦点

    主要有两种解决办法: 1.创建一个临时空数组创建一个临时空数组,render内操作的是这个空数组内的对象,然后监听这个临时空数组,在赋值给table组件的data,render内操作的是这个空数组内的 ...

  10. Windows消息循环

    首先理解一句话:“Windows”向应用程序发送了一条消息.这里是指Windows调用了该程序内部的一个函数. 当UpdateWindow被调用后,新建的窗口在屏幕便完全可见了.此时,Windows会 ...