Interview Question |Possible Words For Digit Digit| Recursion | C++

Kartikeya Mishra
5 min readNov 25, 2023
Interview Question |Possible Words For Digit Digit| Recursion | C++

Code :

vector<string>ans;

string btn[10] ={"abc" , "def" ,"ghi" , "jkl","mno","pqrs","tuv","wxyz"};

void solve(int a[],int n, string s ,int idx){
if(idx == n){
ans.push_back(s);
return;
}

string alpha = btn[a[idx]-2]; //2 no btn ka idx 0 pr data present
for(int i=0 ;i<alpha.length();i++){
solve(a,n,s+alpha[i],idx+1);
}

}

//Function to find list of all words possible by pressing given numbers.
vector<string> possibleWords(int a[], int n)
{
//Your code here

int idx=0;
string s="";
solve(a,n,s,0);
return ans;
}

English :

This C++ code generates all possible words that can be formed by pressing a sequence of numbers on a mobile phone keypad. Each number corresponds to a set of characters, and the goal is to generate all possible combinations of characters for the given sequence of numbers.

Here’s a breakdown of the code:

  1. btn Array: It is an array that stores the possible characters corresponding to each digit on the phone keypad. For example, btn[2] corresponds to "abc," btn[3] corresponds to "def," and so on.
  2. solve Function: This function is a recursive function that generates all possible combinations of characters for the given sequence of numbers.
  • a: An array containing the sequence of numbers.
  • n: The length of the sequence.
  • s: The current string being formed.
  • idx: The current index in the sequence.
  1. The base case (if(idx == n)) checks if we have processed all digits in the sequence. If yes, it adds the current string s to the ans vector.
  2. Inside the function, it retrieves the characters corresponding to the current digit (btn[a[idx]-2]) and iterates over each character. For each character, it makes a recursive call with the updated string s and increments the index idx.
  3. possibleWords Function: This function initializes the starting index (idx), an empty string (s), and then calls the solve function. Finally, it returns the ans vector containing all possible words.

Here’s an example to illustrate how it works:

int main() {
int a[] = {2, 3, 4}; // Example input sequence
int n = sizeof(a) / sizeof(a[0]);


vector<string> result = possibleWords(a, n);
// Displaying the result
for (const string &word : result) {
cout << word << " ";
}
return 0;
}

For the input sequence {2, 3, 4}, the output might include words like "adg," "adh," "adi," "aeg," and so on, representing all possible combinations of characters for the input sequence.

Hindi :

यह C++ कोड एक मोबाइल फ़ोन की कीपैड पर नंबर सीक्वेंस को दबाकर बन सकने वाले सभी संभावित शब्द उत्पन्न करता है। प्रत्येक नंबर का एक सेट के अक्षरों के साथ संबंधित है, और लक्ष्य उन सभी संभावित अक्षर की कॉम्बिनेशन बनाना है जो दिए गए नंबर सीक्वेंस के लिए हो सकती हैं।

यहां कोड का विवरण है:

  1. btn एरे: यह एक एरे है जो प्रत्येक नंबर पर फोन की कीपैड पर संभावित वर्णों को स्टोर करता है। उदाहरण के लिए, btn[2] "abc" को संदर्भित करता है, btn[3] "def" को संदर्भित करता है, और ऐसा ही जारी रहता है।
  2. solve फ़ंक्शन: यह एक रिकर्सिव फ़ंक्शन है जो दिए गए नंबर सीक्वेंस के लिए वर्णों के सभी संभावित कॉम्बिनेशन बनाता है।
  • a: एक ऐसा एरे जिसमें नंबर की सीक्वेंस होती है।
  • n: सीक्वेंस की लंबाई।
  • s: वर्तमान स्ट्रिंग जो बन रही है।
  • idx: वर्तमान सीक्वेंस में वर्तमान सूची।

आधार केस (if(idx == n)) यह जांचता है कि क्या हमने सभी अंकों को प्रोसेस किया है। अगर हाँ, तो यह वर्तमान स्ट्रिंग s को ans वेक्टर में जोड़ता है।

  1. फ़ंक्शन के भीतर, यह वर्तमान डिजिट के लिए वर्णों को प्राप्त करता है (btn[a[idx]-2]) और प्रत्येक वर्ण के लिए एक लूप चलाता है। प्रत्येक वर्ण के लिए, यह वर्तमान स्ट्रिंग s के साथ अपडेट किया जाता है और सूची idx को बढ़ाता है।
  2. possibleWords फ़ंक्शन: यह स्टार्टिंग इंडेक्स (idx), एक खाली स्ट्रिंग (s), और फिर solve फ़ंक्शन को कॉल करता है। अंत में, यह सभी संभावित शब्दों को स्टोर करने वाले ans वेक्टर को लौटाता है।

यहां एक उदाहरण है जो यह कैसे काम करता है:

int main() {
int a[] = {2, 3, 4}; // उदाहरण स्वीकृत सीक्वेंस
int n = sizeof(a) / sizeof(a[0]);

vector<string> result = possibleWords(a, n);
// परिणाम दिखाना
for (const string &word : result) {
cout << word << " ";
}
return 0;
}

इस सीक्वेंस {2, 3, 4} के लिए, आउटपुट में "adg," "adh," "adi," "aeg," इत्यादि शब्द शामिल हो सकते हैं, जो इनपुट सीक्वेंस के लिए सभी संभावित कॉम्बिनेशन्स को प्रस्तुत करते हैं।

Hinglish :

Yeh C++ code mobile phone keypad par numbers press karke jo words ban sakte hain unko generate karta hai. Har number ek set of characters ko represent karta hai, aur goal hai ki diye gaye numbers ki sequence ke liye sabhi possible character combinations ko generate kare.

Code ka breakdown is tarah hai:

  1. btn Array: Ye ek array hai jo har digit par corresponding characters ko store karta hai. For example, btn[2] "abc" ke liye hai, btn[3] "def" ke liye, aur aise hi aage.
  2. solve Function: Ye ek recursive function hai jo diye gaye numbers ki sequence ke liye sabhi possible character combinations ko generate karta hai.
  • a: Ek array jo numbers ki sequence ko represent karta hai.
  • n: Sequence ka length.
  • s: Current ban rahi string.
  • idx: Current index sequence mein.
  1. Base case (if(idx == n)) ye check karta hai ki kya humne sequence ke saare digits process kar liye hain. Agar haan, toh woh current string s ko ans vector mein add karta hai.
  2. Function ke ander, ye current digit ke corresponding characters ko le leta hai (btn[a[idx]-2]) aur har character ke liye ek recursive call karta hai updated string s ke saath, aur index idx ko increment karta hai.
  3. possibleWords Function: Ye function starting index (idx), ek empty string (s), aur fir solve function ko call karta hai. Ant mein, yeh ans vector ko return karta hai jo sabhi possible words ko contain karta hai.

Yahan ek example hai jo ye dikhata hai ki kaise ye kaam karta hai:

int main() {
int a[] = {2, 3, 4}; // Example input sequence
int n = sizeof(a) / sizeof(a[0]);
vector<string> result = possibleWords(a, n);
// Result ko display karna
for (const string &word : result) {
cout << word << " ";
}
return 0;
}

Input sequence {2, 3, 4} ke liye, output mein "adg," "adh," "adi," "aeg," aise words honge, jo input sequence ke liye possible character combinations ko represent karte hain.

Question Link : https://www.geeksforgeeks.org/problems/possible-words-from-phone-digits-1587115620/1?page=1&category=Recursion&sprint=94ade6723438d94ecf0c00c3937dad55&sortBy=submissions

--

--

Kartikeya Mishra

Computer science engineer nomad exploring digital world. Learning and teaching while having fun.