Before the advent of QWERTY keyboards, texts and numbers were placed on the same key. For example, 2 has “ABC” if we wanted to write anything starting with ‘A’ we need to type key 2 once. If we wanted to type ‘B’, press key 2 twice and thrice for typing ‘C’. Below is a picture of such keypad.
Given a keypad as shown in the diagram, and an n digit number, list all words which are possible by pressing these numbers.
Input number: 234 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 Explanation: All possible words which can be formed are (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 If 2 is pressed then the alphabet can be a, b, c, Similarly, for 3, it can be d, e, f, and for 4 can be g, h, i. Input number: 5 Output: j k l Explanation: All possible words which can be formed are (Alphabetical order): j, k, l, only these three alphabets can be written with j, k, l.
Approach: It can be observed that each digit can represent 3 to 4 different alphabets (apart from 0 and 1). So the idea is to form a recursive function. Then map the number with its string of probable alphabets, i.e 2 with “abc”, 3 with “def” etc. Now the recursive function will try all the alphabets, mapped to the current digit in alphabetic order and again call the recursive function for the next digit and will pass on the current output string.
If the number is 23, Then for 2, the alphabets are a, b, c So 3 recursive function will be called with output string as a, b, c respectively and for 3 there are 3 alphabets d, e, f So, the output will be ad, ae and af for the recursive function with output string. Similarly, for b and c, the output will be: bd, be, bf and cd, ce, cf respectively.
- Map the number with its string of probable alphabets, i.e 2 with “abc”, 3 with “def” etc.
- create a recursive function which takes following parameters, output string, number array, current index and length of number array
- If the current index is equal to the length of number array then print the output string.
- Extract the string at digit[current_index] from the Map, where digit is the input number array.
- Run a loop to traverse the string from start to end
- For every index again call the recursive function with the output string concatenated with the ith character of the string and the current_index + 1.
Implementation: Note that input number is represented as an array to simplify the code.
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 Process returned 0 (0x0) execution time : 0.025 s Press any key to continue.
- Time Complexity: O(4n), where n is number of digits in input number.
Each digit of a number has 3 or 4 alphabets, so it can be said that each digit has 4 alphabets as options. If there are n digits then there are 4 options for first digit and for each alphabet of first digit there are 4 options in second digit, i.e for every recursion 4 more recursion is called (if it does not match the base case). So the time complexity is O(4n).
- Space Compelxity:O(1).
As no extra space is needed.
This article is contributed by Jitendra Sangar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Java ArrayList to print all possible words from phone digits
- Find the last two missing digits of the given phone number
- Print all funny words in a string
- Program to print the given digit in words
- Given a sequence of words, print all anagrams together using STL
- Given a sequence of words, print all anagrams together | Set 1
- Given a sequence of words, print all anagrams together | Set 2
- Print all the non-repeating words from the two given sentences
- Print all possible combinations of words from Dictionary using Trie
- Print words of a string in reverse order
- Print all valid words that are possible using Characters of Array
- Print number of words, vowels and frequency of each character
- Java program to print Even length words in a String
- Extract and print words separately from a given Camel Case string
- Print all words matching a pattern in CamelCase Notation Dictonary
- C Program to Print all digits of a given number
- Print first k digits of 1/n where n is a positive integer
- Print all numbers less than N with at-most 2 unique digits
- Print last k digits of a^b (a raised to power b)
- Check if the given string of words can be formed from words present in the dictionary