imashutosh51

Letter Combination of a phone number

Oct 16th, 2022 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. '''
  2. Time complexity = (k^n)*n where k=max count of all the chars for each digit ie.4 for 7='pqrs' and n for string building for all the chars.
  3. '''
  4.  
  5. def fun(num,cur,i,ans):
  6.     if i>=len(num) and cur!="":
  7.         ans.append(cur)
  8.         return
  9.    
  10.     dict={"2":"abc","3":"def","4":"ghi","5":"jkl",'6':"mno","7":"pqrs","8":"tuv","9":"wxyz"}
  11.     current_num=num[i]
  12.     val=dict[current_num]
  13.     print(current_num,cur)
  14.     for k in val:
  15.         fun(num,cur+k,i+1,ans)
  16.  
  17. class Solution:
  18.     def letterCombinations(self, num: str) -> List[str]:
  19.         ans=[]
  20.         fun(str(num),"",0,ans)
  21.         return ans
  22.        
Advertisement
Add Comment
Please, Sign In to add comment