Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Letter Combinations of a Phone Number.cpp #72

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Cpp/Letter Combinations of a Phone Number.cpp
Original file line number Diff line number Diff line change
@@ -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<string>&v, map<int,vector<char>>&m){
if(i==s.length()){
v.push_back(str);
return;
}
for(int j=0; j<m[s[i]-'0'].size(); j++){
str.push_back(m[s[i]-'0'][j]);
find(i+1,s,v,m);
str.pop_back();
}
}
vector<string> letterCombinations(string digits) {
map<int,vector<char>>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);
}
vector<string>v;
if(digits=="") return v;
find(0,digits,v,m);
return v;
}
};