Tags: strings backtracking brute-force
17. Letter Combinations of a Phone Number
- first map the values to the alphabets.
- keep yourself @digit string then move fwd.
- keep storing the values and then recurse them in similar way.
Implementation
```cpp
class Solution {
public:
string s;
vector ans;
map<char, string> mp;
void fun(string digits) {
if (digits.empty()) {
if (!s.empty())
ans.push_back(s);
return;
}
for (const char& ch: mp[digits.front()]) {
s.push_back(ch);
fun(digits.substr(1));
s.pop_back();
}
}
vector letterCombinations(string digits) {
mp['2'] = "abc";
mp['3'] = "def";
mp['4'] = "ghi";
mp['5'] = "jkl";
mp['6'] = "mno";
mp['7'] = "pqrs";
mp['8'] = "tuv";
mp['9'] = "wxyz";
fun(digits);
return ans;
}
};
```
</details>