letter_combinations_of_a_phone_number

 1from itertools import chain
 2
 3
 4# @leet start
 5class Solution:
 6    def letterCombinations(self, digits: str) -> list[str]:
 7        """
 8        Given a string of digits, this question asks us to return the letter
 9        combinations it can create.
10
11        To do this, we first create a mapping of digit -> possible letters.
12
13        Then, for the starting digit, we flatten the list of possible letters
14        and use that as our starting list.
15
16        For every subsequent digit, we iterate through the current combinations
17        and extend the current set by appending each of the letters to all of the
18        current items in our set.
19        """
20        if not digits:
21            return []
22
23        mapping = {
24            "2": "abc",
25            "3": "def",
26            "4": "ghi",
27            "5": "jkl",
28            "6": "mno",
29            "7": "pqrs",
30            "8": "tuv",
31            "9": "wxyz",
32        }
33        for k, v in mapping.items():
34            mapping[k] = list(v)
35
36        res = chain(mapping[digits[0]])
37
38        for c in digits[1:]:
39            new_res = []
40            for combination in res:
41                new_res.extend([combination + letter for letter in mapping[c]])
42            res = new_res
43
44        return res
45
46
47# @leet end
48
49
50def test():
51    assert 2 + 2 == 4
class Solution:
 6class Solution:
 7    def letterCombinations(self, digits: str) -> list[str]:
 8        """
 9        Given a string of digits, this question asks us to return the letter
10        combinations it can create.
11
12        To do this, we first create a mapping of digit -> possible letters.
13
14        Then, for the starting digit, we flatten the list of possible letters
15        and use that as our starting list.
16
17        For every subsequent digit, we iterate through the current combinations
18        and extend the current set by appending each of the letters to all of the
19        current items in our set.
20        """
21        if not digits:
22            return []
23
24        mapping = {
25            "2": "abc",
26            "3": "def",
27            "4": "ghi",
28            "5": "jkl",
29            "6": "mno",
30            "7": "pqrs",
31            "8": "tuv",
32            "9": "wxyz",
33        }
34        for k, v in mapping.items():
35            mapping[k] = list(v)
36
37        res = chain(mapping[digits[0]])
38
39        for c in digits[1:]:
40            new_res = []
41            for combination in res:
42                new_res.extend([combination + letter for letter in mapping[c]])
43            res = new_res
44
45        return res
def letterCombinations(self, digits: str) -> list[str]:
 7    def letterCombinations(self, digits: str) -> list[str]:
 8        """
 9        Given a string of digits, this question asks us to return the letter
10        combinations it can create.
11
12        To do this, we first create a mapping of digit -> possible letters.
13
14        Then, for the starting digit, we flatten the list of possible letters
15        and use that as our starting list.
16
17        For every subsequent digit, we iterate through the current combinations
18        and extend the current set by appending each of the letters to all of the
19        current items in our set.
20        """
21        if not digits:
22            return []
23
24        mapping = {
25            "2": "abc",
26            "3": "def",
27            "4": "ghi",
28            "5": "jkl",
29            "6": "mno",
30            "7": "pqrs",
31            "8": "tuv",
32            "9": "wxyz",
33        }
34        for k, v in mapping.items():
35            mapping[k] = list(v)
36
37        res = chain(mapping[digits[0]])
38
39        for c in digits[1:]:
40            new_res = []
41            for combination in res:
42                new_res.extend([combination + letter for letter in mapping[c]])
43            res = new_res
44
45        return res

Given a string of digits, this question asks us to return the letter combinations it can create.

To do this, we first create a mapping of digit -> possible letters.

Then, for the starting digit, we flatten the list of possible letters and use that as our starting list.

For every subsequent digit, we iterate through the current combinations and extend the current set by appending each of the letters to all of the current items in our set.

def test():
51def test():
52    assert 2 + 2 == 4