[LeetCode][python3]0017. Letter Combinations of a Phone Number

Start the journey
N2I -2020.04.02

1. My first try

class Solution:
    def letterCombinations(self, digits: str) -> List[str]:      
        if not digits:
            return []
        dic={'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']}
        ans=dic[digits[0]]
        for d in digits[1:]:
            ans=self.listxlist(ans,dic[d])       
        return ans
        
    def listxlist(self,a,b):
        #print("hello")
        #print(a,b)
        combine=[]
        for item_a in a:
            for item_b in b:
                combine.append(item_a+item_b)
        return combine

Explanation:

The Solution use a small custom function listxlist to finish the work.


Comments