Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time.
  2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.

Example 1:

Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"] Output: 5 Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

思路

BFS

代码

 class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
// use dict to check duplicats
Set<String> dict = new HashSet<>(wordList);
Queue<String> queue = new LinkedList<>();
queue.add(beginWord);
int level = 0;
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size; i++){
String cur = queue.remove();
if(cur.equals(endWord)){ return level + 1;}
for(int j = 0; j < cur.length(); j++){
// hit -> {'h', 'i', 't'}
char[] charArray = cur.toCharArray();
for(char c = 'a'; c <='z'; c++){
// {'h', 'i', 't'} for'h', try checking 'a','b'...'z' which forms ait, bit...zit
charArray[j] = c;
String temp = new String(charArray);
if(dict.contains(temp)){
queue.add(temp);
// to avoid dead loop, like hit will find hit itself
dict.remove(temp);
}
}
}
}
level++;
}
return 0;
}
}

[leetcode]127. Word Ladder单词接龙的更多相关文章

  1. LeetCode 127. Word Ladder 单词接龙(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

  2. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  3. 127 Word Ladder 单词接龙

    给出两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列,转换需遵循如下规则:    每次只能改变一个字母.    变换过程中的 ...

  4. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  5. Leetcode#127 Word Ladder

    原题地址 BFS Word Ladder II的简化版(参见这篇文章) 由于只需要计算步数,所以简单许多. 代码: int ladderLength(string start, string end, ...

  6. leetcode@ [127] Word Ladder (BFS / Graph)

    https://leetcode.com/problems/word-ladder/ Given two words (beginWord and endWord), and a dictionary ...

  7. leetcode 127. Word Ladder ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  8. [LeetCode] 127. Word Ladder _Medium tag: BFS

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  9. Java for LeetCode 127 Word Ladder

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

随机推荐

  1. cxgrid中回车键光标移到下列

    OptionsBehavior.GoToNextCellOnEnter:=True; 更完善的回车 可以在焦点到了最后一列再回车时有下一行则移到下一行的第一列,没有下一行则新增记录并移到第一列 pro ...

  2. c/c++基础 const

    int main(int argc, char* argv[]) { '; '; a1='; //a2='2'; //a2是字符常量 "; "; a3="; //a4=& ...

  3. Class-reference types 类引用类型--快要失传的技术

    先摘一段原版的说明: A class-reference type, sometimes called a metaclass, is denoted by a construction of the ...

  4. Unity3D脚本学习——运行时类

    AssetBundle 类,继承自Object.AssetBundles让你通过WWW类流式加载额外的资源并在运行时实例化它们.AssetBundles通过BuildPipeline.BuildAss ...

  5. 吴裕雄 python深度学习与实践(6)

    from pylab import * import pandas as pd import matplotlib.pyplot as plot import numpy as np filePath ...

  6. 用git bash 传数据到远程出错:git push origin master 出错:error: failed to push some refs to

    https://blog.csdn.net/qq_28055429/article/details/51007453

  7. 转换es6

    { "presets": [["env", { "modules": false }],"stage-3"," ...

  8. ABAP IMPORT&EXPORT的用法

    1含有事务码 1.1 不注入参数,直接调用 CALL TRANSACTION 'SUIM' AND SKIP FIRST SCREEN. 1.2 注入参数, SET PARAMETER ID: '屏幕 ...

  9. 关于小程序 scroll-view 左右横向滑动没有效果(无法滑动)问题

    https://www.cnblogs.com/miu-key/p/7606024.html

  10. ssh架构之hibernate(一)简单使用hibernate完成CRUD

    1.Hibernate简介   Hibernate是一个开放源代码的对象关系映射(ORM)框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,h ...