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. Pytorch实战学习(八):基础RNN

    <PyTorch深度学习实践>完结合集_哔哩哔哩_bilibili Basic RNN ①用于处理序列数据:时间序列.文本.语音..... ②循环过程中权重共享机制 一.RNN原理 ① X ...

  2. Jmeter面试

    jmeter面试题及答案 1.解释什么是jmeter? jmeter是一款java开源工具,用于性能负载测试.它旨在分析和衡量web应用程序和各种服务的性能和负载功能行为. 2.说明jmeter的工作 ...

  3. 【转载】 VCS编译的基本参数,//code细节,注释

    https://www.bilibili.com/read/cv18255106?spm_id_from=333.999.0.0

  4. (0501) perl-正则表达式-匹配关键字task【知乎】

    (1)

  5. ES 基础操作

    集群 健康值的三种状态 Green:所有索引的所有分片均可用 primary 和 replice 均可用. Yellow 至少有一个 replice不可以用, 但是所有的 primary 正常. Re ...

  6. uni-app 声音/震动提示,播放系统默认消息声音 安卓(Android)测试通过

    可以跟据自己需求使用,有不足的地方希望大家帮忙补充 参数 type Options vibrate:震动 othre:铃声 1 msg_remind(type) 2 { 3 if(type=='vib ...

  7. 打印timescale信息

    verilog中用:$printtimescale($root.tb); systemverilog class中用: $printtimescale($sformatf("%m" ...

  8. mysql报错:MySQL server has gone away

    一.报错提示: 二.报错原因: 原因一: 一种可能是发送的 SQL 语句太长,以致超过了 max_allowed_packet 的大小,如果是这种原因,你只要修改 my.cnf,加大 max_allo ...

  9. idea的小tip

    1. 校验正则表达式 String类型的matches方法中键入option+return选择 check regexp可以测试正则的正确性

  10. 使用CSS 绘制各种形状

      如何使用CSS 绘制各种形状? 很多小伙伴在做开发的时候,遇到一些要画很多形状的时候,就很纠结了,知道怎么用CSS去布局,就是不知道怎么画图案. 其实使用CSS可以绘制出很形状的,比如三角形,梯形 ...