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. Amd,Cmd, Commonjs, ES6 import/export的异同点

    Amd,Cmd, Commonjs, ES6 import/export等均是模块化方案 1.Commonjs使用在Nodejs上,加载模块是同步的. 2.Amd是requirejs在推广过程中对模块 ...

  2. 【ubuntu20】设置静态IP

    sudo gedit /etc/netplan/01-network-manager-all.yaml 修改文件为: # Let NetworkManager manage all devices o ...

  3. aws note

    Amazon EC2 Instance Types https://aws.amazon.com/ec2/instance-types/ My courses - AWS Skill Builder ...

  4. Linux磁盘占满处理

    按一下操作查看大文件在哪里, 清理大文件. 你切换到 / du -sh * 进入占用多的目录,再使用du -sh *找到下一个大目录. 以此类推,删除无用大文件

  5. ThinkPHP获取当前url

    $httpType = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTT ...

  6. 【快问快答】为什么NPOI读取表格数据的时候,遇到空格单元值会直接忽略

    答:其实就是Excel文档的问题,具体问题出在哪里不知道,反正尝试换了一份新的文档来进行导入就可以!

  7. print语法

    循环体 是 缩进的 :缩进是 Python 组织语句的方式.在交互式命令行里,得为每个缩输入制表符或空格.使用文本编辑器可以实现更复杂的输入方式:所有像样的文本编辑器都支持自动缩进.交互式输入复合语句 ...

  8. 在虚拟机安装用友u8的时候出现的一些问题

    几个星期前装的了,小问题不记得,但是有个问题还是记一下 在win10,sql sever2008,u8都装完后,想登录账号,但总是找不到数据源,初始化也失败(密码也不记得自己设过没),也就是登陆时会有 ...

  9. PID模板

    typedef struct{ float Kp,Ki,Kd; float Target; float Current; float Error[3]; float DeadZone; float O ...

  10. 戴尔n4110在win7下无法使用virtualbox的解决方法(应该对win7都有用)

    正文 因为已经学了一段时间的汇编了嘛,想着就拿单独一台机器出来学汇编好了,刚好趁着天气降温回学校拿被子的机会把笔记本也拿出来了,然后我装上了virtual box,把编译好的文件写到虚拟盘中,打开就直 ...