Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

Only one letter can be changed at a time
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
Note:

Return an empty list if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
You may assume no duplicates in the word list.
You may assume beginWord and endWord are non-empty and are not the same.

Example 1:

Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

Output:
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]

Example 2:

Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Output: []

Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

Solution

    type Record struct {
        Word string
        Path []string
    }
    func findLadders(beginWord string, endWord string, wordList []string) [][]string{
        result := [][]string{}
        // lenWL := len(wordList)
        // check edge: end not existed in wordList
        if containsWord(&wordList, endWord) == false{
            return result
        }
        wordList = append(wordList, beginWord)
        minLenPath := math.MaxInt64

        que := []Record{} 
        record := Record{Word: beginWord, Path: []string{beginWord}}
        que = append(que, record)

        level := 1;
        visited := map[string]int{}
        visited[beginWord] = level

        patternDict, patternOptions := genAllPattern(&wordList)

         // fmt.Println(patternDict, patternOptions)

        for len(que) > 0 {
            levelSize := len(que)
            x := 0
            for x < levelSize {
                wordRecord:= (que[0])
                que = que[1:]
                if wordRecord.Word == endWord {
                    // fmt.Println(wordRecord.Path, len(wordRecord.Path))
                    if len(wordRecord.Path) == minLenPath {
                        result = append(result, wordRecord.Path)
                    }
                    if len(wordRecord.Path) < minLenPath {
                       minLenPath =  len(wordRecord.Path)
                        result = result[0:0]
                        result = append(result, wordRecord.Path)
                    }
                }
                options := (*patternOptions)[wordRecord.Word]
                for _, pattern := range options {

                    for _, nextWord := range (*patternDict)[pattern] {
                        if v,existed := visited[nextWord]; existed == false || v == level  {
                            path := make([]string,len(wordRecord.Path))
                            copy(path, wordRecord.Path)
                            path = append(path, nextWord)
                            // fmt.Println(nextWord, path, "!!")
                            newRecord := Record{Word: nextWord, Path: path}
                            que = append(que, newRecord)
                            visited[nextWord] = level
                        }
                    }
                }
                x++
                // change word
            }
            // fmt.Println(level)
            level++ 
        }
        return result
    }


    func containsWord(list *[]string, target string) bool {
        lenL := len(*list)

        for i:= 0; i < lenL; i++ {
            if (*list)[i] == target {
                return true
            }
        }
        return false
    }



    func genAllPattern(list *[]string) (*map[string][]string, *map[string][]string) {
        lenL := len(*list)
        lenW := len( (*list)[0])
        pattenToWords := map[string][]string{}
        wordToPatterns  := map[string][]string{}

        for i:= 0; i < lenL; i++ {
            word := (*list)[i]
            wordToPatterns[(*list)[i]] = []string{}
            for j:= 0; j < lenW; j++ {
                var sBuilder strings.Builder
                sBuilder.WriteString(word[0:j])
                sBuilder.WriteString("*")
                sBuilder.WriteString(word[j+1:])

                pattern := sBuilder.String()
                wordToPatterns[(*list)[i]] = append(wordToPatterns[(*list)[i]], pattern)

                if _,existed := pattenToWords[pattern];existed == false{
                    pattenToWords[pattern] = []string{}
                }
                pattenToWords[pattern] = append(pattenToWords[pattern], (*list)[i])
            } 
        }

        return &pattenToWords, &wordToPatterns
    }