[Leetcode] 17. Letter combinations of a Phone number, Python implementation "Medium" __python

Source: Internet
Author: User
Letter combinations of a Phone number

Given a digit string, return all possible letter combinations this number could represent.

A mapping of Digit to letters (just as the telephone buttons) is given below.

Input:digit string "23"
Output: ["Ad", "AE", "AF", "BD", "Be", "BF", "CD", "CE", "CF"].

The picture is not on the phone, meaning that the 9 House button, 2 corresponding to the abc,3 corresponding Def, so. To give you all the possibilities, it's mathematically called, I forgot ...
is to use recursion or DFS, such as give ' 234 ', first look at 2, that should now be [' A ', ' B ', ' C '],
Plus 3, is it not on the basis of this [' A ', ' B ', ' C '], [' A ', ' B ', ' C '] each followed by a ' d ', ' e ', ' F ': that is, ["Ad", "AE", "AF", "BD", "Be", "BF", " CD ", ce", "CF"],
Then add 4 ' g ' h ' I ', and so on.

Here is the Python implementation, which is cool, the main recursive function on one line. Do not know how many C + + line Ah ...

 class Solution (object): Def lettercombinations (self, digits): "" ": Type Digi TS:STR:RTYPE:LIST[STR] "" if digits = = ': return [] self.
            Digitdict=[', ' 1 ', ' abc ', ' Def ', ' Ghi ', ' jkl ', ' mno ', ' PQRS ', ' TUV ', ' wxyz '] res = ['] for D in digits:
        res = SELF.LETTERCOMBBT (int (d), Res) return res def LETTERCOMBBT (self, digit, oldstrlist): return [dstr+i to I in self. Digitdict[digit] for dstr in Oldstrlist] 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.