A classic BFS problem, that is, we use a Queue to store temporary solution and record the size of the current Queue.

For each node in this BFS tree, we try k times of transformation, k is the length of the word.

We record every word which already has been seen in case of a loop. Thus we need a HashSet.

public class Solution {
// use BFS to solve
// in every level of BFS searching,
// we find the word that replaced by one char and also in the dict without have seen before
// data structure: seen(HashSet), cur(Queue)
// assume that the wordList is not null, case insensitive, no duplicate, every word is non-empty
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
// Write your solution here
HashSet<String> seen = new HashSet<>();
Queue<String> cur = new LinkedList<>();
seen.add(beginWord);
cur.offer(beginWord);
int cnt = 1;
while(cur.size()>0){
cnt++;
int size = cur.size();
for(int i=0; i<size; i++){
String todo = cur.poll();
for(int j=0; j<todo.length(); j++){
List<String> changed= getQualified(todo, j, wordList, seen);
for(String s: changed){
if(String.valueOf(s)==String.valueOf(endWord)){
return cnt;
}else{
cur.offer(s);
seen.add(s);
}
}
}
}
}
return 0;
} private List<String> getQualified(String from, int ignore, List<String> wordList, HashSet<String> seen){
List<String> res = new ArrayList<>();
for(String s: wordList){
int flag = 0;
for(int i=0; i<from.length(); i++){
if(i!=ignore && from.charAt(i)!=s.charAt(i)){
flag=1;
}
}
if(flag==0 && !seen.contains(s)){
res.add(s);
}
}
return res;
}
}

127. Word Ladder via java的更多相关文章

  1. 127. Word Ladder(M)

    127. Word LadderGiven two words (beginWord and endWord), and a dictionary's word list, find the leng ...

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

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

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

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

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

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

  5. Leetcode#127 Word Ladder

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

  6. 【LeetCode】127. Word Ladder

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  7. Java for LeetCode 127 Word Ladder

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

  8. leetcode 127. Word Ladder ----- java

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

  9. 127 Word Ladder 单词接龙

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

  10. leetcode 126. Word Ladder II ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

随机推荐

  1. C#笔记誊录<二>

    c#编译器把源码编译成IL(中间)代码,再由CLR通过即时编译器把IL代码编译成本机机器语言(平台语言) www.webkaka.com//测试服务器的网速 ctrl+k+d 代码对其 CTRL+K+ ...

  2. uniapp输入空格

    uniapp   密码框输入空格(去除空格)的时候一直回显不及时  经过一番折腾 终于搞定 1.先赋值:     this.pwd = e.detail.value 2. 使用setTimeout(再 ...

  3. WPF-UI框架MahApps.Metro使用教程

    参考教程:https://www.shuzhiduo.com/A/xl561ZaoJr/ 一,MahApps.Metro安装 1,项目中引用"MahApps.Metro.dll"[ ...

  4. Python学习笔记(四)算术运算符

    一.算术运算符 运算符 说明 实例 结果 + 加 12.45 + 15 27.45 - 减 4.56 - 0.26 4.3 * 乘 5 * 3.6 18.0 / 除法(和数学中的规则一样) 7 / 2 ...

  5. 【根文件系统 】开发板通过nfs挂载ubuntu上的根文件系统

    在此基础上制作可用的根文件系统:https://www.cnblogs.com/hengqiu/p/15908597.html 1.ubuntu搭建nfs服务器 sudo apt-get instal ...

  6. oracle 索引创建

    --查询表里的索引 select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.inde ...

  7. CH32V307/CH32V203 IO翻转速度测试

    CH32V307/CH32V203 IO极限翻转测试 记录RISC-V MCU CH32V307/CH32V203 在144MHz主频.-Os优化下,IO极限翻转频率. GPIO初始化代码如下: /* ...

  8. 三.database阶段回顾

    阶段回顾: 1.mysql:文件管理软件 2.三部分: 服务端 sql语句 客户端 3.客户端 mysql navicat 4.授权操作 用户操作 授权操作 5.sql语句 数据库操作 create ...

  9. 项目开发中的ORM框架使用mybatis还是mybatis-plus

    mybatis支持xml配置文件和注解 mybaits-plus也支持xml配置文件和注解,多了baseMapper,将基础的CRUD操作单独拿出来进行了封装 mybatis是一款优秀的持久层框架,它 ...

  10. Twenty-six

    条件渲染指令 v-if:动态移除或创建元素(如果刚进入页面的时候,某些元素默认不需要被显示,而且后期这个元素很可能也不需要被展示出来) v-show: 添加或移除display:none的样式(频繁切 ...