-
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.
Merge pull request #460 from Shubhra-Narang/main
Create solution.cpp
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
Day-19/q3-Letter Combinations of a Phone Number/Shubhra-Narang--c.md
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,33 @@ | ||
``` | ||
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; | ||
} | ||
}; | ||
``` |