问题描述:
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:
- Only one letter can be changed at a time
- Each intermediate word must exist in the word list
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog"
,
return its length 5
.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
原问题链接:https://leetcode.com/problems/word-ladder/
问题分析
如果不是充分利用问题中提到的所有字符都假定为小写的话,问题会显得更加复杂,解决思路也更加不好找。在给定的字符串里,我们如果要转换成下一个字符串,可能它里面的每个字符都变化了一个,那么对于它转换的下一个目标字符,就有它字符个数n x 26那么多种可能。而具体哪一种能最快或者说最近的转换成目标字符呢,这确实不好说。
这里我们可以利用一个树的广度优先遍历的思路,首先从beginWord的第一个字符开始,设置它的值从'a'到'z',然后来判断它是否在给定的集合wordList里。当然,因为我们这么转换可能会变成前面已经转换过的元素了,所以这种情况要避免。为了避免这种情况,需要定义一个Map<String, Integer>,这里的key表示已经遍历过的元素,而且这些元素也存在于wordList中。value则表示是第几次转换。
另外,既然是要按照广度遍历的方式,我们需要将每次生成的符合条件的字符串加入到一个队列中,以方便后面的进一步转换。
所以,详细的代码实现如下:
public class Solution { public int ladderLength(String beginWord, String endWord, Set<String> wordList) { Map<String, Integer> map = new HashMap<>(); map.put(beginWord, 1); Queue<String> queue = new LinkedList<>(); queue.add(beginWord); while(!queue.isEmpty()) { String word = queue.remove(); if(word.equals(endWord)) break; for(int i = 0; i < word.length(); i++) { char[] array = word.toCharArray(); for(char c = 'a'; c <= 'z'; c++) { array[i] = c; String tmp = new String(array); if(wordList.contains(tmp) && !map.containsKey(tmp)) { map.put(tmp, map.get(word) + 1); queue.add(tmp); } } } } if(!map.containsKey(endWord)) return 0; return map.get(endWord); } }
相关推荐
正确的姿势,学习的态度来刷 LeetCode:高效的代码、简洁的注释、精炼的总结。
leetcode 非官方顺序leetcode题解,主要代码为Python和C++。 leetcode 第1题: leetcode 第2题: leetcode 第3题: leetcode 第4题: leetcode 第5题: leetcode 第6题: leetcode 第7题: leetcode 第9题: ...
leetcode11 top 1. 位运算 LeetCode191 : 二进制位1的个数 LeetCode338 : 比特位运算 2. 字典树 LeetCode209 : 实现一个Trie结构 LeetCode79 : 单词搜索(判断单词是否出现在给定的网格中) LeetCode212 : 单词搜索II...
LeetCode::laptop:LeetCode解决方案
leetcode 答案 leetCode :keyboard:我的 Leetcode 解题答案
lru缓存leetcode 力码 涵盖了 Geeks for Geeks 和 Leet Code 的各种问题。 LeetCode 1 : 二和 (46_Easy) LeetCode 2 : 两个数字相加 (96_Medium) LeetCode 3 : 无重复字符的最长子串 (214_Medium) LeetCode 4 : 两个...
LeetCode 在LeetCode和其他编码平台上解决的问题的集合
leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。 下面的目录中 $number 题号代表经典 LeetCode 题目,$number.$number 题号表示 LeetCode 面试题。 :receipt: 目录 统计 值 AC 的...
LeetCode 101:和你一起你轻松刷题(C++)
Leetcode:Leetcode提交
leetcode 分类 LeetCode :bouquet::bouquet::bouquet: 介绍 leetcode 题解,Issues 会记录 leetcode 解题之路,并使用 label 进行了分类。 目录 链表
:fire: Leetcode :fire: 实践使完美 :party_popper: 开玩笑的单元测试 :sparkles: 简单的代码 :artist_palette: 可读代码 入门指南 git clone https: //github.com/tangweikun/leetcode.git cd leetcode npm ...
idea中leetcode插件Rust 中的 LeetCode 解决方案 怎么跑?...,所有解决方案代码都在leetcode::leetcode::editor::en并重用于leetcode 。 它有一个全局结构Solution ,所有解决方案条目都在其中实现。
leetcode:leetcode刷题
Leetcode:LeetCode解题代码
LeetCode:LeetCode的代码
LeetCode:LeetCode的注释
leetcode:LeetCode问题
leetcode:LeetCode题解
加油站问题leetcode LeetCode LeetCode-JS分类列表: :smiling_face_with_smiling_eyes: :flushed_face: :winking_face: :face_with_tongue: :face_with_open_mouth: :beaming_face_with_smiling_...