Created
July 22, 2020 04:04
-
-
Save cywang117/56230d47e0ebb577b049c1f39eaea64a to your computer and use it in GitHub Desktop.
Word Ladder - Whiteboarding exercise - HR RPT21
This file contains hidden or 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
// Whiteboard exercise: Word Ladder: https://leetcode.com/problems/word-ladder/description/ | |
// NOTE: Interviewer did not progress into asking me for actual code, even though I actually | |
finished pseudocode halfway through my sesion. Therefore I'm selecting the 'finished core concepts' | |
option in Google forms. | |
// Input: | |
// beginWord = "hit", | |
// endWord = "cog", | |
// wordList = ["hot","dot","dog","lot","log","cog"] | |
// Output: 5 | |
// Input: | |
// beginWord = "hit" | |
// endWord = "cog" | |
// wordList = ["hot","dot","dog","lot","log"] | |
// Output: 0 | |
// Explanation: The endWord "cog" is not in wordList, therefore no possible transformation. | |
// Input: beginWord, endWord, wordList (array of strings) | |
// Output: shortest transformation count from beginning to end | |
// Constraints: each word aside from beginWord must be in the wordList, | |
// all words same length, lowercase a-z letters, assume that words in wordList are same as begin/endWord | |
// Edge cases: endWord is not in wordList, empty wordList | |
// Example | |
// Input: | |
// beginWord = "hit", | |
// endWord = "cog", | |
// wordList = ["hot","dot","dog","lot","log","cog"] | |
// hit -> hot -> dot -> dog -> cog | |
// hit: hot // remove hot | |
// hot: dot, lot // remove dot, lot | |
// dot: dog // remove dog | |
// dog: cog // return count | |
// lot: log // remove log | |
// Output: 5 | |
// Helper function: | |
// findNextWords: word, wordList -> subList of words that are one character off | |
// Initialize a list of possible nextWords | |
// For each letter in word | |
// For each word in wordList | |
// If word has same letter in the same spot as our word | |
// If one other character has the same letter as our word, add this word in wordList to nextWords | |
// Return nextWords | |
// Main function: | |
// if endWord not in wordList, return 0 | |
// Initialize a counter | |
// Initialize an array of transformation counts | |
// Recursive function: curWord, curWordList, counter | |
// if curWord is endWord, add counter to array | |
// findNextWords(curWord, curWordList) -> subList | |
// Remove curWord and all words in subList from curWordList | |
// For each of the words in the subList | |
// Call recursive function with (word, copy of curWordList, incremented counter) | |
// Call recursive function with initial args (beginWord, copy of wordList, initial counter) | |
// Return the minimum from array of transformation counts, or 0 if array is empty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment