dialpad combinations Algorithm

A dialpad combinations algorithm is a computational method used to generate all possible combinations or permutations of characters that can be formed using the keys on a telephone dial pad. Each key on the dial pad, except for 1 and 0, corresponds to a set of letters (and sometimes symbols) in addition to its numeric value. For example, the key '2' represents the letters 'A', 'B', and 'C', while the key '3' stands for 'D', 'E', and 'F'. By pressing a sequence of keys, a user can form a combination of characters that could potentially represent words or abbreviations. The dialpad combinations algorithm aims to find all possible character combinations given a specific input of numeric keys. The algorithm works by iteratively exploring each key in the input sequence, and recursively generating all possible combinations for the remaining keys. It starts with an empty string and appends the characters corresponding to each key in a depth-first manner. As it processes each key, the algorithm combines the current character with the combinations generated from the remaining keys, eventually producing all possible character combinations for the given input. The dialpad combinations algorithm can be implemented using various programming techniques such as recursion, backtracking, or dynamic programming. This algorithm has practical applications in various domains, such as word prediction in text messaging systems, generating vanity phone numbers, and decoding encoded messages using a dial pad as the input mechanism.
/*
 * You are given a digit string (e.g "1234", "567" etc), 
 * provide all possible letter combinations we could generate from this digit string,
 * based on the mapping we see on the telphone/mobile dialpad. 
 * If you have typed SMS in old style phones, you would know. For e.g. "1" is mapped to "abc", 
 * 2 is mapped to "def". 
 * Example: "34" will give output: {"dg","dh","di","eg","eh","ei","fg","fh","fi"}
 * Note that order does not matter in result set.
 * 
 * Approach:
 * 
 * We can use a simple backtracking approach. Since each digit letter could be mapped to multiple characters,
 * for example '1' is mapped to 'a', 'b' and 'c'. As we move along in the digit string, for an index i, we will
 * add all possible letters to the string and recursively move ahead to i+1, 
 * i.e. we will choose say 'a' as our choice for the iteration, we map '1' to 'a' and then move to next
 * digit letter (i+1) and recurse, then we will make 'b' as our choice for position i, and then 'c'
 * This way we do it for all the letters at all the positions. 
 * The idea is to generate all possible subsets of the string, keeping in mind the order.
 */

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

std::unordered_map<char, std::string> dict {
    {'1' , "" },
    {'2' , "abc"},
    {'3' , "def"},
    {'4' , "ghi"},
    {'5' , "jkl"},
    {'6' , "mno"},
    {'7' , "pqrs"},
    {'8' , "tuv"},
    {'9' , "wxyz"},
    {'0' , " "},
    {'*' , "*"},
    {'#' , "#"}
};

void helper(const std::string& digits, std::string rs,
    std::vector<std::string>& result, int index)
{
    if (index == digits.length()) {
        result.push_back(rs);
        return;
    }
    
    char c = digits[index];
    std::string ps = dict[c];
    for (int i = 0; i < ps.length(); ++i) {
        helper(digits, rs + ps[i], result, index + 1);
    }
}

std::vector<std::string> letter_combinations(const std::string& digits)
{
    std::vector<std::string> result;
        if (digits.size() == 0) {
            return result;
        }
        std::string rs("");
        helper(digits, rs, result,  0);
        return result;
}

void print_vec(const std::vector<std::string>& vec)
{
    for(auto v: vec) {
        std::cout << v << " ";
    }
    std::cout << std::endl;
}
int main()
{
    std::string digits{"34"};
    std::cout << "All possible combinations of digits " << digits << " are: \n";
    print_vec(letter_combinations(digits));
    return 0;
}

LANGUAGE:

DARK MODE: