Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
What are the possible characters in the words? All lowercase alphabet letters.
Do all words have the same number of characters?
Could the beginning or end word be empty?
Could the beginning word be the same as the ending word?
HAPPY CASE
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
EDGE CASE
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Output: 0
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For graphs, some of the top things we want to consider are:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the wordList using BFS, starting from beginWord until we either reach the end or see endWord. As we visit each node, we add all adjacent nodes by looking up if a certain word exists in wordList.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
private static final char[] letters = "abcdefghijklmnopqrstuvwxyz".toCharArray();
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
// create a set of all words in wordList
HashSet<String> wordsSet = new HashSet(wordList);
HashSet<String> visited = new HashSet<>();
Queue<Pair<String, Integer>> queue = new LinkedList();
// start BFS from beginWord, setting currentWord
queue.offer(new Pair<>(beginWord, 1));
while (!queue.isEmpty()) {
Pair<String, Integer> pair = queue.poll();
String word = pair.getKey();
int length = pair.getValue();
if (word.equals(endWord)) {
return length;
}
// for each index of current word and each letter not equal to current word at index
for (int i = 0; i < word.length(); i++) {
for (int j = 0; j < letters.length; j++) {
if (word.charAt(i) != letters[j]) {
String candidate = word.substring(0,i) + letters[j] + word.substring(i+1);
if (wordsSet.contains(candidate) && !visited.contains(candidate)) {
queue.offer(new Pair<>(candidate, length + 1));
visited.add(candidate);
}
}
}
}
}
// if endWord was not found, return 0
return 0;
}
LETTERS = set('abcdefghijklmnopqrstuvwxyz')
def word_ladder(start, end, words):
# create a set of all words in wordList
words = set(words)
# create a set of visited nodes
visited = set()
queue = [(start, 1)] # queue for BFS, which stores the word and distance
while len(queue) > 0:
word, length = queue.pop(0)
if word == end:
return length
for i, char in enumerate(word):
for letter in LETTERS:
if char != letter:
candidate = word[:i] + letter + word[i + 1:]
if candidate in words and candidate not in visited:
queue.append([candidate, length + 1])
visited.add(candidate)
# if endWord was not found, return 0
return 0
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Time Complexity: O(M^2 * N), where M is the length of words and N is the total number of words in the input word list
Space Complexity: O(M^2 * N) to store all MM transformations for each of the NN words in the set or dictionary