leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 9fce40e7e010e496dce11e684915d7d648135538
parent 059292e8e6c05a3e3465a3df1a447e559031cdd8
Author: Andrew Laack <andrew@laack.co>
Date:   Wed, 16 Jul 2025 14:40:16 -0500

Completed letter combinations of a phone number

Diffstat:
Aletter-combinations-of-a-phone-number/letter-combinations-of-a-phone-numberV1.py | 37+++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+), 0 deletions(-)

diff --git a/letter-combinations-of-a-phone-number/letter-combinations-of-a-phone-numberV1.py b/letter-combinations-of-a-phone-number/letter-combinations-of-a-phone-numberV1.py @@ -0,0 +1,37 @@ +class Solution: + def letterCombinations(self, digits: str) -> List[str]: + + if digits == "": + return [] + + lookup = { + 2: ["a", "b", "c"], + 3: ["d", "e", "f"], + 4: ["g", "h", "i"], + 5: ["j", "k", "l"], + 6: ["m", "n", "o"], + 7: ["p", "q", "r", "s"], + 8: ["t", "u", "v"], + 9: ["w", "x", "y", "z"], + } + opts = options([], lookup, digits, 0) + + final_res = ["".join(opt) for opt in opts] + + return final_res + + +def options(current, lookup, digits, index): + if index >= len(digits): + return [current] + + current_opts = lookup[int(digits[index])] + ret_ls = [] + + for opt in current_opts: + ls_cp = current.copy() + ls_cp.append(opt) + results = options(ls_cp, lookup, digits, index + 1) + for result in results: + ret_ls.append(result) + return ret_ls