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. Markdown的在线使用

    欢迎使用 Markdown在线编辑器 MdEditor Markdown是一种轻量级的「标记语言」 Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容 ...

  2. 01_Node的版本管理

    Node的版本管理工具 常见的node的版本管理工具有两种N.NVM 但是他们有一个致命的问题就是不支持Windwos 但是NVM延申了一个nvm-windows的版本,他就可以很好的支持window ...

  3. 使用go自定义生成项目LISENSE(授权协议)

    需要使用一个使用go开发的工具,叫license,在Windows下安装这个工具,请确保你使用的go sdk是1.16以上的版本,然后执行下面的命令: go install github.com/ni ...

  4. centos7上安装使用docker环境

    系统环境准备: docker 运行在centos7上,要去系统为64位.系统内核版本为3.10以上 1.查看系统版本:cat/etc/redhat-release 2.查看系统内核版本:uname - ...

  5. python multiprocessing多进程 cannot pickle '_io.TextIOWrapper' object

    Python 3.9.6 在windows下使用multiprocessing多进程报如下错误,但linux下正常 Traceback (most recent call last): File &q ...

  6. 获取git远程分支仓库

    1:新建本地目录 2:进入并初始化这个目录 git init 3:新建一个文件,并添加 git add . 4: git commit -m "first commit" 5:新建 ...

  7. .Net Core 中使用NLog替代默认日志

    1.添加引用nlog.config和Nlog.Web.AspNetCore 2.配置NLog 配置文件 添加一个Web配置文件xxxx.Config <?xml version="1. ...

  8. 吴恩达老师机器学习课程chapter09——异常检测

    吴恩达老师机器学习课程chapter09--异常检测 本文是非计算机专业新手的自学笔记,高手勿喷. 本文仅作速查备忘之用,对应吴恩达(AndrewNg)老师的机器学期课程第十五章. 目录 吴恩达老师机 ...

  9. 搭建webssh和nginx反向代理

    0.前置条件,centos主机安装了python3和nginx   1.安装webssh pip3 install webssh   2.添加自定义服务 systemctl cat webssh # ...

  10. vue - 解决页面跳转到外部链接后,ios回退到本页面,生命周期,路由导航守卫钩子都无法触发的问题

    虽然加班,最近心情还是可以的,没多少bug找上门. 乐级生悲,遇到了个奇葩的坑,花了点时间百度才解决...但我离不开的'板砖'是安卓的,对此坑是抗拒的,完全没问题... #此坑描述 ...订单详情某按 ...