LeetCode – Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that only one letter can be changed at a time and each intermediate word must exist in the dictionary. For example, given:
start = "hit" end = "cog" dict = ["hot","dot","dog","lot","log"]
One shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog", the program should return its length 5.
Java Solution
This is a search problem, and breath-first search guarantees the optimal solution.
class WordNode{ String word; int numSteps; public WordNode(String word, int numSteps){ this.word = word; this.numSteps = numSteps; } } public class Solution { public int ladderLength(String beginWord, String endWord, Set<String> wordDict) { LinkedList<WordNode> queue = new LinkedList<WordNode>(); queue.add(new WordNode(beginWord, 1)); wordDict.add(endWord); while(!queue.isEmpty()){ WordNode top = queue.remove(); String word = top.word; if(word.equals(endWord)){ return top.numSteps; } char[] arr = word.toCharArray(); for(int i=0; i<arr.length; i++){ for(char c='a'; c<='z'; c++){ char temp = arr[i]; if(arr[i]!=c){ arr[i]=c; } String newWord = new String(arr); if(wordDict.contains(newWord)){ queue.add(new WordNode(newWord, top.numSteps+1)); wordDict.remove(newWord); } arr[i]=temp; } } } return 0; } } |
<pre><code> String foo = "bar"; </code></pre>
-
SM TechWorld
-
Alik Elzin
-
Alik Elzin
-
onewithsix
-
Aman Gupta
-
Mamed
-
Arvind Raj
-
Mihai
-
Mohammad Tbeishat
-
Hoc Ngo
-
Howard Wang
-
indiver kumar
-
Bismoy Murasing
-
lekzeey
-
Steve Dyson
-
Steve Dyson
-
Sohrab Ahmad
-
Eugene Arnatovich
-
Dhanaraj D
-
Surbhi Motghare
-
subash sethy
-
Roman
-
CRH
-
peng li
-
Jayesh
-
Leonardo Campos
-
Alibek Datbayev
-
Dado
-
Stephen Boesch
-
Stephen Boesch
-
dwelo
-
Nooby
-
ryanlr
-
ryanlr
-
zhuoran
-
Curious Guy
-
Cong
-
hdante
-
BK
-
BK
-
dfgd
-
Guanting
-
ryanlr
-
Shaochen Huang
-
Yifan Peng
-
Sole
-
AlgorithmFreak
-
Tex
-
Tex
-
ajay
-
hbrong
-
Dun Liu
-
Kae Pajunar
-
jason
-
elvalord
-
74s0nf
-
Csnerds
-
Csnerds