Golang solution (0ms)


  • 0
    func letterCombinations(digits string) (result []string) {
        if len(digits) == 0 { return result }
        table := []string{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}
        letterCombinationsHelper(digits, table, 0, []byte{}, &result)
        return result
    }
    
    func letterCombinationsHelper(digits string, table []string, index int, combination []byte, result *[]string) {
        if index == len(digits) {
            *result = append(*result, string(combination))
            return
        }
        word := table[digits[index] - '0']
        for i := 0; i < len(word); i++ {
            letterCombinationsHelper(digits, table, index + 1, append(combination, word[i]), result)
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.