Word Ladder Solutions in Go
Number 127
Difficulty Medium
Acceptance 29.7%
Link LeetCode
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodefunc ladderLength(beginWord string, endWord string, wordList []string) int {wordMap, que, depth := getWordMap(wordList, beginWord), []string{beginWord}, 0for len(que) > 0 {depth++qlen := len(que)for i := 0; i < qlen; i++ {word := que[0]que = que[1:]candidates := getCandidates(word)for _, candidate := range candidates {if _, ok := wordMap[candidate]; ok {if candidate == endWord {return depth + 1}delete(wordMap, candidate)que = append(que, candidate)}}}}return 0}func getWordMap(wordList []string, beginWord string) map[string]int {wordMap := make(map[string]int)for i, word := range wordList {if _, ok := wordMap[word]; !ok {if word != beginWord {wordMap[word] = i}}}return wordMap}func getCandidates(word string) []string {var res []stringfor i := 0; i < 26; i++ {for j := 0; j < len(word); j++ {if word[j] != byte(int('a')+i) {res = append(res, word[:j]+string(rune(int('a')+i))+word[j+1:])}}}return res}