aboutsummaryrefslogtreecommitdiffstats
path: root/17-go/main.py
blob: 6e997addf674221a328ecbde12a45919c0ebbd4a (plain) (blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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()