Formatted question description: https://leetcode.ca/all/17.html
17 Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. Note: Although the above answer is in lexicographical order, your answer could be in any order you want. @June.18,2017: variation-1: input with duplicate digits "222222", then it's a duplicate reduction process maybe need to pre-sort the charArray of string variation-2: now the result combination string length is the same as in put string but result string length could from 1 to input string length also duplicate issue. "222" -> a,b,c,aa,ab,ac,bb,bc,cc,aaa,aab,aac,....ccc I'll use set of string to avoid duplicated string... @tag-string
Similar topics include Path Sum II, Subsets II, Permutations, Permutations II, Combinations, Combination Sum and Combination Sum II and so on.
Here you can use recursive Recursion to solve, you need to build a dictionary to store the string represented by each number, and then you need a variable level, which records the number of characters in the currently generated string, and realizes the routine and the above questions. similar. In the recursive function, the level is first judged. If the number of digits in digits is equal, the current combination is added to the result res, and then returned. We pass the number in digits to the dict to extract the string, then traverse the extracted string, add each character to the current combination, and call the recursive function.