Skip to content

Instantly share code, notes, and snippets.

@heanfig
Created February 24, 2017 16:36
Show Gist options
  • Save heanfig/91d4e68c5e50f37f079bd8f415b9a656 to your computer and use it in GitHub Desktop.
Save heanfig/91d4e68c5e50f37f079bd8f415b9a656 to your computer and use it in GitHub Desktop.
/*
Given two words, beginWord and endWord, and a wordlist of approved words, find the length of the shortest transformation sequence from beginWord to endWord such that:
Only one letter can be changed at a time
Each transformed word must exist in the wordList.
Return the length of the shortest transformation sequence, or 0 if no such transformation sequence exists.
Note: beginWord does not count as a transformed word. You can assume that beginWord and endWord are not empty and are not the same.
Example
For beginWord = "hit", endWord = "cog", and wordList = ["hot", "dot", "dog", "lot", "log", "cog"], the output should be
wordLadder(beginWord, endWord, wordList) = 5.
The shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog" with a length of 5.
Input/Output
[time limit] 4000ms (js)
[input] string beginWord
Constraints:
1 ≤ beginWord.length ≤ 5.
[input] string endWord
Constraints:
endWord.length = beginWord.length.
[input] array.string wordList
An array containing all of the approved words. All words in the array are the same length and contain only lowercase English letters. You can assume that there are no duplicates in wordList.
Constraints:
2 ≤ wordList.length ≤ 600,
wordList[i].length = beginWord.length.
[output] integer
An integer representing the length of the shortest transformation sequence from beginWord to endWord using only words from wordList as described above.
*/
//Read More: http://www.programcreek.com/2012/12/leetcode-word-ladder/
function wordLadder(beginWord, endWord, wordList) {
var linkedList = new LinkedList();
linkedList.push(new WordNode(beginWord, 1));
wordList.push(endWord);
while(linkedList.length()!=0){
var top = linkedList.remove(0);
var word = top.word;
if(word == endWord){
return top.steps;
}
var arr = word.split("");
for(var i=0; i<arr.length; i++){
for(var j=("a".charCodeAt()); j<("z".charCodeAt()); j++){
var temp = arr[i];
if(arr[i] != String.fromCharCode(j)){
arr[i] = String.fromCharCode(j);
}
var newWord = arr.join("");
if(~wordList.indexOf(newWord)){
linkedList.push(new WordNode(newWord, top.steps+1));
var index = wordList.indexOf(newWord);
wordList.splice(index,1);
}
arr[i] = temp;
}
}
}
//console.log(splitarray);
return 0;
}
function WordNode(w,st){
this.word = w;
this.steps = st;
}
var LinkedList = function(e){
var that = {}, first, last;
that.push = function(value){
var node = new Node(value);
if(first == null){
first = last = node;
}else{
last.next = node;
last = node;
}
};
that.length = function(){
var count = 0;
var current = first, previous;
while(current != null){
count++;
current=current.next;
}
return count;
}
that.pop = function(){
var value = first;
first = first.next;
return value;
};
that.remove = function(index) {
var i = 0;
var current = first, previous;
if(index === 0){
//handle special case - first node
first = current.next;
}else{
while(i++ < index){
//set previous to first node
previous = current;
//set current to the next one
current = current.next
}
//skip to the next node
previous.next = current.next;
}
return current.value;
};
var Node = function(value){
this.value = value;
var next = {};
};
return that;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment