(This question is a variation of the LeetCode question 17. Letter Combinations of a Phone Number. If you haven't completed that question yet, it is recommended to solve it first.)
You are given a string of digits (0–9) representing key presses on a mobile phone keypad. Each digit maps to a set of letters as follows:
- '1' → ['A', 'B', 'C']
- '2' → ['D', 'E', 'F']
- '3' → ['G', 'H', 'I']
- '4' → ['J', 'K', 'L']
- '5' → ['M', 'N', 'O']
- '6' → ['P', 'Q', 'R']
- '7' → ['S', 'T']
- '8' → ['U', 'V', 'W']
- '9' → ['X', 'Y']
- '0' → ['Z']
Consecutive identical digits may be treated as separate individual presses or grouped presses. The number of times a digit appears consecutively determines which letter is selected from its corresponding mapping.
However, the group size cannot exceed the number of letters mapped to that digit. For example, the press of "111" can be interpreted as "AAA" (three separate presses), "AB" or "BA" (groupings of one and two), or "C" (a group of three).
Implement a function that returns all possible letter combinations by partitioning the string into valid groups and mapping each group to its letter, and you may return the results in any order.
Constraints
- The input string contains at least one digit.
- Each character in the input string is a digit between 0 and 9.
- Only consecutive identical digits can be grouped together.
- The size of any group must not exceed the number of letters available in the digit's mapping.
- The order of the letters in the resulting combinations follows the order of the digits in the input string.
Example 1:
Input: "7772" Output: ["SSSD","STD","TSD"] Explanation: The first group can be formed by taking one or more consecutive '7's:
- Grouping as ["7", "7", "7", "2"]: Each single '7' maps to 'S' and '2' maps to 'D' → "SSSD".
- Grouping as ["7", "77", "2"]: The first '7' gives 'S', the group "77" gives 'T', followed by 'D' → "STD".
- Grouping as ["77", "7", "2"] → "TSD".
- ["777", "2"] is not a valid group since there is no letter mapping to three consecutive '7'.
Example 2:
Input: "2222" Output: ["DDDD", "DDE", "DED", "DF", "EDD", "EE", "FD"]
Example 3:
Input: "123456" Output: ["ADGJMP"]
