Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
All combinations of strings that can be used to dial a number in C/C++?
Given a phone number, this program generates all possible string combinations that can be formed using the traditional phone keypad mapping. Each digit (2-9) maps to multiple letters, while digits 0 and 1 map to themselves.
Syntax
void printWords(int number[], int n);
Keypad Mapping
Example: Phone Number to String Combinations
This example generates all possible letter combinations for the phone number "234" −
#include <stdio.h>
#include <string.h>
/* Keypad mapping for digits 0-9 */
const char keypad[10][5] = {
"0", "1", "ABC", "DEF", "GHI", "JKL",
"MNO", "PQRS", "TUV", "WXYZ"
};
/* Recursive function to generate all combinations */
void generateCombinations(int number[], int curr_digit, char output[], int n) {
/* Base case: if all digits processed, print the combination */
if (curr_digit == n) {
printf("%s ", output);
return;
}
/* Get current digit and its corresponding letters */
int digit = number[curr_digit];
/* Try all possible characters for current digit */
for (int i = 0; i < strlen(keypad[digit]); i++) {
output[curr_digit] = keypad[digit][i];
generateCombinations(number, curr_digit + 1, output, n);
/* For digits 0 and 1, only one character exists */
if (digit == 0 || digit == 1)
return;
}
}
/* Wrapper function to initialize and call recursive function */
void printAllCombinations(int number[], int n) {
char result[n + 1];
result[n] = '\0'; /* Null terminate the string */
generateCombinations(number, 0, result, n);
printf("\n");
}
int main() {
int number[] = {2, 3, 4};
int n = sizeof(number) / sizeof(number[0]);
printf("Phone number: ");
for (int i = 0; i < n; i++) {
printf("%d", number[i]);
}
printf("\nAll possible combinations: ");
printAllCombinations(number, n);
return 0;
}
Phone number: 234 All possible combinations: 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
How It Works
The algorithm uses recursive backtracking to generate all combinations −
- Base Case: When all digits are processed, print the current combination
- Recursive Case: For each digit, try all possible letters mapped to it
- Backtracking: After exploring one path, backtrack to try other possibilities
- Special Handling: Digits 0 and 1 map only to themselves
Time Complexity
The time complexity is O(4^n) in the worst case, where n is the number of digits. This occurs when all digits are 7 or 9 (which have 4 letters each). For other digits with 3 letters, the complexity is O(3^n).
Conclusion
This recursive approach efficiently generates all possible letter combinations for a phone number using the traditional T9 keypad mapping. The algorithm handles all digits correctly, including the special cases of 0 and 1.
