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. 9.java单链表初学代码复现及一些不值一提的小问题(2)

    首先写完了update和delete函数,在之前的铺垫下.倒是不难,结构和之前的都相同,遍历找到节点后处理该节点.代码如下 public void update(teamNode node){ tea ...

  2. vue学习之-----组件递归调用

    1.关键点 2.父组件 <template> <div> <div class="btn-title"> <el-button @clic ...

  3. NGINX一次电脑自己可以访问其它IP访问不了

    配制好NGINX 本地电脑curl http..... 正常访问...其它电脑不可以 第一想法防火墙 查一下  firewall-cmd --state not running 然后查下是不是服务开启 ...

  4. linux中用crontab定时任务启动jar无效

    修改前脚本内容如下: #!/bin/bash nohup java -Xms512m -Xmx512m -jar /opt/jar/xx-0.0.1-SNAPSHOT.jar & 检查了各方面 ...

  5. 哲讯分享:sap软件多少钱一套

    SAP软件一般指SAP. SAP,为"System Applications and Products"的简称,是德国SAP公司的产品--企业管理解决方案的软件名称.至今世界500 ...

  6. RocketMq 下载安装

    下载地址 https://rocketmq.apache.org/zh/download linux安装步骤 启动nameserver bin目录下启动nameserver nohup sh mqna ...

  7. hive元起动报错:Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V

    错误原因: 1.系统找不到这个类所在的jar包 2.jar包的版本不一样系统不知道使用哪个. hive启动报错的原因是后者 解决办法: 1.com.google.common.base.Precond ...

  8. H5云图后台读写CAD文件-在线CAD,网页CAD,网页浏览编辑CAD

    说明 后台提供MxFileConvert.exe程序,它可以将CAD图纸转换成前台能加载显示的格式,程序调用方法见: https://help.mxdraw.com/?pid=32中的"如何 ...

  9. Salesforce Connect 连接两个不同的Org(实际设置方法)

    利用Salesforce的标准功能:Salesforce Connect,可以轻松的将两个组织(Org)连接起来.实现Object的共享(包括参照和编辑). 要求: ①两个组织必须是开发者Edtion ...

  10. Android Studio 生成Jar包以及是否混淆打包等ZengYuanFinn博客等你来查看

    1,Android studio生成jar包的前提是要确保生成的代码是引用的module工程: 2,在需要生成jar包的build.gradle(上图倒数第三行)中添加如下代码: //生成jar包 t ...