In numeric keypads, texts and numbers are placed on the same key. For example, 2 has "ABC", if we want to write something starting with "A", we must enter key 2 once. If we would like to type “B,” press the key 2 twice and three times to enter “C. This keyboard is shown below.
keyboard http://d2o58evtke57tz.cloudfront.net/wp-content/uploads/phoneKeyboard.png
Considering the keyboard, as shown in the diagram, and the number of n digits, list all the words that are possible by clicking these numbers.
For example, if the input number is 234, possible words that can be formed (in alphabetical order): adg adh adi aeg aeh aei afg afh afi bdg bdh bdi beg beh bei bfg bfh bfi cdg cdh cdi ceg ceh cei cfg cfh cfi
First we do some calculations. How many words are possible with seven digits with each digit representing n letters? For the first digit, we have no more than four options, and for each choice for the first letter, we have no more than four options for the second digit, and so on. Therefore, his simple math will be O (4 ^ n). Since the keys 0 and 1 do not have the corresponding alphabet, and many characters have 3 characters, 4 ^ n will be the upper limit of the number of words, not the minimum words.
Now let's do some examples.
For the number above 234. Do you see any pattern? Yes, we notice that the last character is always either G, H or I, and whenever it resets its value from I to G, the number to the left of it changes. Similarly, when the second last alphabet resets its meaning, the third last alphabet receives changes and so on. The first character is reset only once, when we generated all the words. This can be seen from the other end. That is, whenever a symbol at position i changes, a symbol at position i + 1 passes through all possible symbols and creates a ripple effect until we reach the end. Since 0 and 1 do not have any characters associated with them. we must break down because there will be no iteration for these numbers.
Let's take the second approach, as it will be easy to implement with recursion. We go to the end and come back one after another. Great condition for recursion. allows you to search for the base register. When we get to the last character, we print the word with all possible characters for the last digit and return. Simple base register. When we get to the last character, we print the word with all possible characters for the last digit and return. Simple base case.
The following is an implementation of the C recursive approach for printing the entire possible word corresponding to an input number n digits. Please note that the input number is presented as an array to simplify the code.
#include <stdio.h> #include <string.h> // hashTable[i] stores all characters that correspond to digit i in phone const char hashTable[10][5] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; // A recursive function to print all possible words that can be obtained // by input number[] of size n. The output words are one by one stored // in output[] void printWordsUtil(int number[], int curr_digit, char output[], int n) { // Base case, if current output word is prepared int i; if (curr_digit == n) { printf("%s ", output); return ; } // Try all 3 possible characters for current digir in number[] // and recur for remaining digits for (i=0; i<strlen(hashTable[number[curr_digit]]); i++) { output[curr_digit] = hashTable[number[curr_digit]][i]; printWordsUtil(number, curr_digit+1, output, n); if (number[curr_digit] == 0 || number[curr_digit] == 1) return; } } // A wrapper over printWordsUtil(). It creates an output array and // calls printWordsUtil() void printWords(int number[], int n) { char result[n+1]; result[n] ='\0'; printWordsUtil(number, 0, result, n); } //Driver program int main(void) { int number[] = {2, 3, 4}; int n = sizeof(number)/sizeof(number[0]); printWords(number, n); return 0; }
Output:
adg adh adi aeg aeh aei afg afh afi bdg bdh bdi beg beh bei bfg bfh bfi cdg cdh cdi ceg ceh cei cfg cfh cfi
Time complexity:
The complexity of the time above the code is O (4 ^ n), where n is the number of digits in the input number.
Literature:
http://www.flipkart.com/programming-interviews-exposed-secrets-landing-your-next-job-3rd/p/itmdxghumef3sdjn?pid=9788126539116&affid=sandeepgfg