Leetcode - Letter Combinations of a Phone Number
https://leetcode.com/problems/letter-combinations-of-a-phone-number/
Given a string containing digits from 2-9
inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | func letterCombinations(digits string) []string { if len(digits) == 0 { return []string{} } hash := map[byte][]byte{ '2' : []byte{ 'a' , 'b' , 'c' }, '3' : []byte{ 'd' , 'e' , 'f' }, '4' : []byte{ 'g' , 'h' , 'i' }, '5' : []byte{ 'j' , 'k' , 'l' }, '6' : []byte{ 'm' , 'n' , 'o' }, '7' : []byte{ 'p' , 'q' , 'r' , 's' }, '8' : []byte{ 't' , 'u' , 'v' }, '9' : []byte{ 'w' , 'x' , 'y' , 'z' }, } result := []string{} combine(digits, hash, 0 , "" , &result) return result } func combine(digits string, hash map[byte][]byte, curr int , tmp string, result *[]string) { if curr == len(digits) { *result = append(*result, tmp) return } for _, c := range hash[digits[curr]] { combine(digits, hash, curr+ 1 , tmp + string(c), result) } } |