Created
May 30, 2016 18:35
-
-
Save jianminchen/3d8e5ad6042dce7e019cf11c0b1eee22 to your computer and use it in GitHub Desktop.
Leetcode 126 - word ladder - http://www.programcreek.com/2014/06/leetcode-word-ladder-ii-java/ with a test case, code works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace _126LadderLength_WarmupV | |
{ | |
class WordNode | |
{ | |
public String word; | |
public int numSteps; | |
public WordNode pre; | |
public WordNode(String word, int numSteps, WordNode pre) | |
{ | |
this.word = word; | |
this.numSteps = numSteps; | |
this.pre = pre; | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string beginWord = "hit"; | |
string endWord = "cog"; | |
string[] arr = new string[5] { "hot", "dot", "dog", "lot", "log" }; | |
HashSet<string> wordList = new HashSet<string>(arr); | |
List<List<string>> result = findLadders(beginWord, endWord, wordList); | |
} | |
/* | |
* http://www.programcreek.com/2014/06/leetcode-word-ladder-ii-java/ | |
* | |
* From Java Code to C# code | |
* | |
*/ | |
public static List<List<String>> findLadders(String start, String end, ISet<String> dict) | |
{ | |
List<List<String>> result = new List<List<String>>(); | |
LinkedList<WordNode> queue = new LinkedList<WordNode>(); | |
queue.AddLast(new WordNode(start, 1, null)); | |
dict.Add(end); // add end word into HashSet | |
int minStep = 0; | |
HashSet<String> visited = new HashSet<String>(); | |
HashSet<String> unvisited = new HashSet<string>(dict); | |
int preNumSteps = 0; | |
while (queue.Count > 0) | |
{ | |
// get used to practice using LinkedList in C# (double linked list), | |
// Java LinkedList is more popular | |
WordNode top = queue.First(); | |
queue.RemoveFirst(); | |
String word = top.word; | |
int currNumSteps = top.numSteps; | |
if (word.CompareTo(end) == 0) | |
{ | |
if (minStep == 0) | |
{ | |
minStep = top.numSteps; | |
} | |
if (top.numSteps == minStep && minStep != 0) | |
{ | |
//nothing | |
List<String> t = new List<String>(); | |
t.Add(top.word); | |
while (top.pre != null) | |
{ | |
// use List<string> as an array, iterator - 0 | |
t.Add(top.pre.word); | |
top = top.pre; | |
} | |
result.Add(t); | |
continue; | |
} | |
} | |
if (preNumSteps < currNumSteps) | |
{ | |
unvisited.ExceptWith(visited); // first time, use HashSet ExceptWith api | |
} | |
preNumSteps = currNumSteps; | |
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 (unvisited.Contains(newWord)) | |
{ | |
queue.AddLast(new WordNode(newWord, top.numSteps + 1, top)); | |
visited.Add(newWord); | |
} | |
arr[i] = temp; | |
} | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment