Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void fun(string digits,unordered_map<char,string> mp,vector<string> &ans,string cur,int i,int n){
- if(i<n){
- for(int k=0;k<mp[digits[i]].size();k++){
- fun(digits,mp,ans,cur+mp[digits[i]][k],i+1,n);
- }
- }
- else{
- ans.push_back(cur);
- return;
- }
- }
- class Solution {
- public:
- vector<string> letterCombinations(string digits) {
- vector <string> ans;
- if(digits.size()==0) return ans;
- unordered_map<char,string> mp;
- mp['2']="abc";
- mp['3']="def";
- mp['4']="ghi";
- mp['5']="jkl";
- mp['6']="mno";
- mp['7']="pqrs";
- mp['8']="tuv";
- mp['9']="wxyz";
- fun(digits,mp,ans,"",0,digits.size());
- return ans;
- }
- };
- #Python
- ans=[]
- def fun(st,i,cur):
- print(i,len(st))
- global ans;
- dict={'2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz'}
- if i==len(st):
- ans.append(cur)
- return
- else:
- temp=dict[st[i]]
- for k in range(len(temp)):
- fun(st,i+1,cur+temp[k])
- class Solution:
- def letterCombinations(self, digits: str) -> List[str]:
- if len(digits)==0:
- return []
- global ans;
- ans=[]
- fun(digits,0,'')
- return ans;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement