diff --git a/Cpp/Letter Combinations of a Phone Number.cpp b/Cpp/Letter Combinations of a Phone Number.cpp new file mode 100644 index 0000000..92511a9 --- /dev/null +++ b/Cpp/Letter Combinations of a Phone Number.cpp @@ -0,0 +1,37 @@ +/* +Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. + +A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. +*/ + +class Solution { +public: + string str=""; + void find(int i, string s, vector&v, map>&m){ + if(i==s.length()){ + v.push_back(str); + return; + } + for(int j=0; j letterCombinations(string digits) { + map>m; + int j=1; + for(int i=0; i<26; i++){ + if(i%3==0 && i<18){ + j++; + } + if(i==19) j++; + if(i==22) j++; + m[j].push_back('a'+i); + } + vectorv; + if(digits=="") return v; + find(0,digits,v,m); + return v; + } +};