127. Word Ladder via java
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的更多相关文章
- 127. Word Ladder(M)
127. Word LadderGiven two words (beginWord and endWord), and a dictionary's word list, find the leng ...
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- LeetCode 127. Word Ladder 单词接龙(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...
- [LeetCode] 127. Word Ladder 单词阶梯
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- Leetcode#127 Word Ladder
原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...
- 【LeetCode】127. Word Ladder
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- Java for LeetCode 127 Word Ladder
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- leetcode 127. Word Ladder ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- 127 Word Ladder 单词接龙
给出两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列,转换需遵循如下规则: 每次只能改变一个字母. 变换过程中的 ...
- leetcode 126. Word Ladder II ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
随机推荐
- 物联网IOT定位技术详解
早在15世纪,当人类开始探索海洋的时候,定位技术也随之催生.当时的定位方法十分粗糙,就是是运用航海图和星象图以确定自己的位置. 随着社会的进步和科技的发展,定位技术在技术手段.定位精度.可用性等方面均 ...
- LENGTH,LENGTHB
LENGTH 1.语法 length(string) 2.说明: 统计字段的字符长度 3.示例: select length('abcd') from dual; --返回值:4 select len ...
- 3月1日至3月2日——数据结构与算法分析阅读笔记,线性表,AI。
(开头是一些废话啊,最近感觉学习状态不太好,上高数的时候左耳听进去右耳就出来了,有点跟不上,可能是没吃饭的原因,也可能是最近强度有点大了,下午上完课就给自己休息了一下,结果刷手机全是关于AI的内容,谢 ...
- java okio 找不到的问题
问题描述: okio 找不到的问题 解决办法: 下载 jar_files.zip 在idea-File-Project Structure- Project Settings - Libraries ...
- qgis中的时间格式化函数
为方便自己查询,尤其是年月日等在不同编程语言直接的差异,就从官方doc中截图放这
- EasyUI异步Tree默认请求id获取不到问题
在做淘淘商城项目过程中,在新增商品-选择类目时,使用EasyUI的异步Tree功能,根据视频教程封装了一个common pojo--EUTreeNode对象,属性值取parentId(id).text ...
- sprinboot多个子模块下 依赖包没有找到 解决方案
最近因为在使用springboot开发项目,在开发过程中,发现自己的子模块导入通用的模块 在启动中 说找不到这个类 百度下 说我要在pom文件下 pulus 插件 那里 加上这段代码 <conf ...
- pgsql 查询结果转换为json数组
select array_to_json(array_agg(row_to_json(t))) from (SELECT * FROM test) t
- 前端电商 sku 的全排列算法
需求 需求描述起来很简单,有这样三个数组: let names = ["iPhone",'iPhone xs'] let colors = ['黑色','白色'] let stor ...
- linux安装IB驱动
一.准备 1.Linux操作系统7.6(根据实际情况变更,此处用redhat7.6系统举例) 2.驱动:MLNX_OFED_LINUX-4.6-1.0.1.1-rhel7.6-x86_64.tgz(根 ...