code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

13 Letter Combinations Of A Phone Number

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const letterCombinationsOfAPhoneNumber = digits => {
  if (digits.length < 1) {
    return []
  }
  let result = []
  let map = {
    '2': 'abc',
    '3': 'def',
    '4': 'ghi',
    '5': 'jkl',
    '6': 'mno',
    '7': 'pqrs',
    '8': 'tuv',
    '9': 'wxyz',
  }
  const backtrack = (index, curr) => {
    if (curr.length == digits.length) {
      result.push(curr.join(''))
      return
    }
    const possibleCombincation = map[digits[index]]
    for (let i = 0; i < possibleCombincation.length; i++) {
      curr.push(possibleCombincation[i])
      backtrack(index + 1, curr)
      curr.pop()
    }
  }
  backtrack(0, [])
  return result
}

console.log(letterCombinationsOfAPhoneNumber('23'))

Created 2022-02-28T07:35:19+00:00 · Edit