Skip to content

Instantly share code, notes, and snippets.

@priyankvex
Created May 13, 2019 13:21
Show Gist options
  • Save priyankvex/e3270b994709c0b891b2ecbb4e0c53af to your computer and use it in GitHub Desktop.
Save priyankvex/e3270b994709c0b891b2ecbb4e0c53af to your computer and use it in GitHub Desktop.
Letter combinations from numbers
"""
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