Advertisement
Singasking

Untitled

Jul 24th, 2023
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. int f(int i,int j,vector<vector<int>>&dp,string &s) {
  2. if(i>j) return 0;
  3. if(dp[i][j]!=-1) return dp[i][j];
  4. int ways=0;
  5. int count=0;
  6. for(int ind=i;ind<min(i+2,j);ind++) {
  7. if((ind==i && s[ind]=='0' )||(ind==i+1 && s[ind-1]=='0')) continue;
  8. if(ind==i+1&&( (s[ind-1]-'0')*10+(s[ind]-'0')>26)) continue;
  9. ways += 1 + f(ind+1,j,dp,s);
  10. count+=1;
  11. }
  12. if(count==0) {
  13. return 0;
  14. exit(0);
  15. }
  16.  
  17. return dp[i][j]=ways;
  18. }
  19.  
  20.  
  21. int Solution::numDecodings(string A) {
  22. //j = A.length()
  23. vector<vector<int>> dp(A.length()+1,vector<int>(A.length()+1,-1));
  24. int ans=f(0,A.length(),dp,A);
  25. if(ans<0) return 0;
  26. return ans;
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement