Friedrich Ewald My Personal Website

Leetcode: Letter Combinations of a Phone Number

letters = {
  '2': ['a', 'b', 'c'],
  '3': ['d', 'e', 'f'],
  '4': ['g','h','i'],
  '5': ['j','k','l'],
  '6': ['m','n','o'],
  '7': ['p','q','r','s'],
  '8': ['t','u','v'],
  '9': ['w','x','y','z']
}

class Solution:
  def letterCombinations(self, digits: str) -> List[str]:
    combinations = []
    self.inner(digits, '', combinations)
    return combinations
  
  def inner(self, digits: str, path: str, combos: list[str]):
    if digits == '':
      if path != '':
        combos.append(path)
      return

    first = digits[0]
    for d in letters[first]:
      self.inner(digits[1:], path + d, combos)


About the author

is an experienced Software Engineer with a Master's degree in Computer Science. He started this website in late 2015, mostly as a digital business card. He is interested in Go, Python, Ruby, SQL- and NoSQL-databases, machine learning and AI and is experienced in building scalable, distributed systems and micro-services at multiple larger and smaller companies.