[InterviewBit] Letter Phone

Letter Phone

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
unordered_map<char,string> mp {{'1',"1"}, {'2',"abc"}, {'3',"def"},
{'4',"ghi"},{'5',"jkl"},{'6',"mno"},
{'7',"pqrs"},{'8',"tuv"},{'9',"wxyz"},
{'0',"0"}};

void helper(vector<string>& res, string& A, int p, string& now) {
if(p == A.size()) res.push_back(now);
else {
for(auto ch : mp[A[p]]) {
now.push_back(ch);
helper(res,A,p+1,now);
now.pop_back();
}
}
}
vector<string> Solution::letterCombinations(string A) {
vector<string> res;
helper(res, A, 0, string() = "");
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/23/PS/interviewbit/letter-phone/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.