#include <bits/stdc++.h>
using namespace std;
class Solution {
private:
void solve(string output , vector<string>&ans , int index ,vector<string>&mapping , string &digits){
if(index>=digits.size()){
ans.push_back(output);
return;
}
int number = digits[index] - '0';
if (number < 2 || number >9){
return;
}
string values = mapping[number];
for(char c : values){
output.push_back(c);
solve(output , ans , index+1, mapping , digits);
output.pop_back();
}
}
public:
vector<string> letterCombinations(string digits) {
if(digits.empty()) return {};
string output = "";
int index = 0;
vector<string>ans;
vector<string>mapping = {" ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
solve(output,ans,index,mapping,digits);
return ans;
}
};
int main() {
Solution sol;
// Input string for which we want to compute combinations
string digits = "23";
// Call the method to get the letter combinations
vector<string> result = sol.letterCombinations(digits);
// Output the result
cout << "Letter Combinations for input \"23\":\n";
for (const string& str : result) {
cout << str << " ";
}
cout << endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBTb2x1dGlvbiB7CiAgICBwcml2YXRlOiAKICAgIHZvaWQgc29sdmUoc3RyaW5nIG91dHB1dCAsIHZlY3RvcjxzdHJpbmc+JmFucyAsIGludCBpbmRleCAsdmVjdG9yPHN0cmluZz4mbWFwcGluZyAsIHN0cmluZyAmZGlnaXRzKXsKICAgICAgICBpZihpbmRleD49ZGlnaXRzLnNpemUoKSl7CiAgICAgICAgICAgIGFucy5wdXNoX2JhY2sob3V0cHV0KTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpbnQgbnVtYmVyID0gZGlnaXRzW2luZGV4XSAtICcwJzsKICAgICAgICBpZiAobnVtYmVyIDwgMiB8fCBudW1iZXIgPjkpewogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIHN0cmluZyB2YWx1ZXMgPSBtYXBwaW5nW251bWJlcl07CiAgICAgICAgZm9yKGNoYXIgYyA6IHZhbHVlcyl7CiAgICAgICAgIC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