-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8f7510
commit 5b43d78
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
Day-19/q3-Letter Combinations of a Phone Number/solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Solution { | ||
public: | ||
void generate(string &digits, int i,string &t, vector<string>&ans, vector<vector<char>>&m){ | ||
if(i==digits.size()){ | ||
ans.push_back(t); | ||
return; | ||
} | ||
for(int j=0;j<m[digits[i]-'2'].size();j++){ | ||
t+=m[digits[i]-'2'][j]; | ||
generate(digits,i+1,t,ans,m); | ||
t.pop_back(); | ||
} | ||
} | ||
vector<string> letterCombinations(string digits) { | ||
vector<string>ans; | ||
int n=digits.size(); | ||
if(n==0)return ans; | ||
vector<vector<char>>m; | ||
m.push_back({'a','b','c'}); | ||
m.push_back({'d','e','f'}); | ||
m.push_back({'g','h','i'}); | ||
m.push_back({'j','k','l'}); | ||
m.push_back({'m','n','o'}); | ||
m.push_back({'p','q','r','s'}); | ||
m.push_back({'t','u','v'}); | ||
m.push_back({'w','x','y','z'}); | ||
string t=""; | ||
generate(digits,0,t,ans,m); | ||
return ans; | ||
} | ||
}; |