T9 Keyboard
Application of Backtracking

Algorithms and Data Structures: TheAlgorist.com

System Design: SystemsDesign.Cloud

Low Level Design: LowLevelDesign.io

Frontend Engineering: FrontendEngineering.io
Problem Statement:
Given a string containing digits from 29 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example 1:
Input: digits = "23"
Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]
Example 2:
Input: digits = ""
Output: []
Example 3:
Input: digits = "2"
Output: ["a","b","c"]
Solution:
 NOTE: I highly recommend going through the Backtracking chapters in the order they are given in the Index page to get the most out of it and be able to build a rocksolid understanding.
You need to get yourself familiar with Backtracking Template before going through the solution, if you haven't done that already.
Let's say digits[0...(n1)] is the given phone number.
We can solve this problem by doing an exhaustive search.
We start from digits[i] where i = 0.
Candidates for the ith character of the solution would all the possible letters for digits[i]. For each of these candidates, candidates for (i + 1)th character of the solution would all possible letters for digits[i + 1]. We go on doing this till we have processed for the last character of the solutions.
What we just described above is nothing but Backtracking. Using our backtracking template we get the below code:
Java Code:
This is a Premium content.
Please subscribe to Algorithms course to access the code.
Python Code:
This is a Premium content.
Please subscribe to Algorithms course to access the code.
Don't forget to take indepth look at the below problems because that is what would make you comfortable with using the backtracking template and master the art of Backtracking:
 Letter Case Permutation
 Power Set
 All Paths Between Two Nodes
 Word Search
 Sudoku
 NQueens
 Word Square
 Generate Parentheses
Instructor:
If you have any feedback, please use this form: https://thealgorists.com/Feedback.
Follow Us On LinkedIn