Created
May 27, 2016 22:29
-
-
Save jianminchen/8d9aeaaa8ad98fd1bc4a78237118506c to your computer and use it in GitHub Desktop.
Leetcode 127 - word ladder - using Queue, BFS - breadth first search - pass Leetcode code online judge
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 Leetcode127WordLadder | |
{ | |
class Program | |
{ | |
/* | |
* Leetcode 127: Word Ladder I | |
* problem statement: | |
* https://leetcode.com/problems/word-ladder/ | |
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. | |
*/ | |
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); | |
int length = ladderLength(beginWord, endWord, wordList); | |
} | |
/* | |
* All are lowercase characters | |
* string is alias of System.String | |
* | |
* Using Queue to do BFS search | |
*/ | |
public static int ladderLength( | |
string beginWord, | |
string endWord, | |
ISet<string> wordDict | |
) { | |
if (beginWord.CompareTo(endWord) == 0 ) return 0; | |
HashSet<string> words = new HashSet<string>(); | |
words = new HashSet<string>(wordDict); | |
words.Add(endWord); | |
// use two queue to track the change and length in the same pace | |
Queue<string> q = new Queue<string>(); | |
Queue<int> qLen = new Queue<int>(); | |
q.Enqueue(beginWord); | |
qLen.Enqueue(0); | |
int length = 1; | |
bool found = false; | |
while (q.Count > 0 && !found) { | |
String removed = q.Dequeue(); | |
length = qLen.Dequeue() + 1; | |
List<String> neighbors = wordNeighbor(removed); | |
foreach (string anb in neighbors) { | |
if (words.Contains(anb)) { // only considers the remaining words. | |
if (anb.CompareTo(endWord) == 0 ) | |
return length + 1; | |
q.Enqueue(anb); | |
qLen.Enqueue(length); | |
words.Remove(anb); // remove word from dictionary - HashSet! | |
} | |
} | |
} | |
return 0; | |
} | |
/* | |
* wordNeighbor: | |
* hit - what is size of List<string> as "hit" word's neighbors | |
* Go throgh each char in "hit" string, | |
* h, can be replaced by any char from 'a' to 'z', 26 in total. | |
* (any replacement of 'h') + "it" - 26 words, | |
* same applies to 'i' and 't', | |
* Total: 26 * 3 = 78 neighbors in List<string> | |
*/ | |
private static List<string> wordNeighbor(string word) { | |
List<string> result = new List<string>(); | |
for (int i = 0; i < word.Length; i++) { | |
char[] array = word.ToCharArray(); | |
for (char indexChar = 'a'; indexChar <= 'z'; indexChar ++) | |
{ | |
array[i] = indexChar; // replace the ith char in the array | |
result.Add(new string(array)); | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment