Created
May 13, 2019 13:21
-
-
Save priyankvex/6d1bb5f77c058d4e097757699b998a32 to your computer and use it in GitHub Desktop.
Letter combinations from numbers
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
""" | |
https://scammingthecodinginterview.com | |
Week 4: Strings | |
Problem: 1 | |
""" | |
class Solution(object): | |
def solve(self, numbers): | |
mapping = { | |
'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', | |
'6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz' | |
} | |
if len(numbers) == 0: | |
return [] | |
if len(numbers) == 1: | |
return list(mapping[numbers[0]]) | |
previous_combinations = self.solve(numbers[:-1]) | |
current_numbers = mapping[numbers[-1]] | |
complete_conbinations = [ | |
comb + s for s in current_numbers for comb in previous_combinations | |
] | |
return complete_conbinations | |
if __name__ == "__main__": | |
a = "23" | |
ans = Solution().solve(a) | |
print(sorted(ans)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment