Friday, September 12, 2014

Leetcode: Letter Combinations of a Phone Number @Python

class Solution:
    # @return a list of strings, [s1, s2]
    def letterCombinations(self, digits):
        dic={'1':'','2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz','0':''}
        rst=['']
        for i in digits:
            rst=[j+k for j in rst for k in dic[i]]
        return rst

No comments :

Post a Comment