Created
October 7, 2024 20:58
-
-
Save nullset2/47d4c540b161c44f31640bce18743c51 to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import "fmt" | |
var letters = map[rune]string{ | |
'2': "abc", | |
'3': "def", | |
'4': "ghi", | |
'5': "jkl", | |
'6': "mno", | |
'7': "pqrs", | |
'8': "tuv", | |
'9': "wxyz", | |
'0': "", | |
'1': "", | |
} | |
func aux(i int, original string, accumulator string, set []string) []string { | |
if len(original) == len(accumulator) { | |
set = append(set, accumulator) | |
return set | |
} | |
for _, l := range letters[rune(original[i])] { | |
current := accumulator + string(l) | |
set = aux(i+1, original, current, set) | |
current = current[:len(current)-1] | |
} | |
return set | |
} | |
func generateCombinations(s string) []string { | |
return aux(0, s, "", []string{}) | |
} | |
func main() { | |
fmt.Println(generateCombinations("234")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment