forked from SnowScriptWinterOfCode/LeetCode_Q
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
day 19-q3 added in python SnowScriptWinterOfCode#418
- Loading branch information
1 parent
9947198
commit 8d5cc37
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
Day-19/q3-Letter Combinations of a Phone Number/Tech-neophyte--p.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
## Approach: | ||
<br /> 1. Initialize a Mapping: Create a dictionary that maps each digit from 2 to 9 to their corresponding letters on a telephone's buttons. | ||
<br /> 2. Base Case: If the input string digits is empty, return an empty list. | ||
<br /> 3. Iteratively make Combinations: Start with an empty combination in a list and iteratively build the combinations by processing each digit in the input string. | ||
For each existing combination, append each corresponding letter for the current digit, building new combinations. | ||
<br /> 4. Result: Return the generated combinations as the final result. | ||
## Python code: | ||
``` | ||
class Solution: | ||
def letterCombinations(self, digits: str) -> List[str]: | ||
if not digits: | ||
return [] | ||
phone_map = { | ||
'2': 'abc', | ||
'3': 'def', | ||
'4': 'ghi', | ||
'5': 'jkl', | ||
'6': 'mno', | ||
'7': 'pqrs', | ||
'8': 'tuv', | ||
'9': 'wxyz' | ||
} | ||
combinations=[""] | ||
for i in digits: | ||
new_comb=[] | ||
for j in combinations: | ||
for l in phone_map[i]: | ||
new_comb.append(j+l) | ||
combinations=new_comb | ||
return combinations | ||
``` |