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 ...
随机推荐
- Linux 第七节( 磁盘配额,RAID )
vim /etc/fstab //编辑文件 UUID=xxxxxxxxxxxxxx /boot xfs defaults,uquota 0 0 //增加uqota命令,允许磁盘扩容 ino ...
- 003. html篇之《表单》
html篇之<表单> 一.结构 <form action="url" method="post" name=""> ...
- iis发布后设置文件夹用户安全权限
发布iis后异常截图: 401 - Unauthorized: Access is denied due to invalid credentials.You do not have permissi ...
- Linux的top命令原理简单了解
top命令描述机器的cpu.内存等状态信息. 每3s刷新一次. 是procps工具集中的一个,该工具集还包括free.ps等等 top命令的代码实现逻辑是:由内核动态生成一个伪文件系统,提供一个内核状 ...
- 【笔记】GTK的bind函数的参数
自打用了cinnamon之后 无比想念gnome的扩展 虽然都是基于gjs的东西 但是gnome的插件在cinnamon上没有就很痛苦 这次修改了个插件 recents 记录历史打开的文件 想添加个功 ...
- css 选择器再学一遍记录一下*1
1 <!DOCTYPE html> 2 <html> 3 <HEAD> 4 5 <style> 6 *{font-size: 12px; padding ...
- linux 安装 talib 的完美姿势!
安装 TA-Lib $ wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz $ tar -zxvf ta-li ...
- awk引用外部变量
test]# cat tmp.tmp120.4987 12.717858119.801948 13.38588119.424529 14.024871119.337438 15.070484119.2 ...
- Unit mysqld.service could not be found.
具体命令 service mysqld status systemctl status mysqld 结果 Unit mysqld.service could not be found. 查看mysq ...
- SSR,SSAO
3D Game Shaders For Beginners Screen Space Reflection (SSR)https://lettier.github.io/3d-game-shaders ...