From 262e6c16bd6c45166738ba520fc58520ceb491c8 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Mon, 5 Dec 2022 12:12:16 +0330 Subject: 17 --- 17-go/main.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 17-go/main.py diff --git a/17-go/main.py b/17-go/main.py new file mode 100755 index 0000000..6e997ad --- /dev/null +++ b/17-go/main.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +from typing import List + + +class Solution: + def letterCombinations(self, digits: str) -> List[str]: + combinations: List[str] = [] + if digits is None or len(digits) == 0: + return combinations + + def find_combinations( + combinations, + digits, + previous, + index, + letters_numbers_mapping, + ): + if index == len(digits): + combinations.append(previous) + return + letters = letters_numbers_mapping[int(digits[index])] + for i in range(0, len(letters)): + find_combinations( + combinations, + digits, + previous + letters[i], + index + 1, + letters_numbers_mapping, + ) + + letters_numbers_mapping = [ + "", + "", + "abc", + "def", + "ghi", + "jkl", + "mno", + "pqrs", + "tuv", + "wxyz", + ] + find_combinations(combinations, digits, "", 0, letters_numbers_mapping) + return combinations + + +def main(): + solution = Solution() + print(solution.letterCombinations("23")) + + +if __name__ == "__main__": + main() -- cgit v1.2.3