Advertisement
Singasking

Untitled

Jul 24th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 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) {
  8. if(s[i]=='0') continue;
  9. }
  10. if(ind==i+1) {
  11. int no = (s[i]-'0')*10+(s[i]-'0');
  12. if(no>26) continue;
  13. if(no<10) continue;
  14. if(s[i]=='0') continue;
  15. }
  16. ways += 1 + f(ind+1,j,dp,s);
  17. count+=1;
  18. }
  19. if(count==0) {
  20. return 0;
  21. exit(0);
  22. }
  23.  
  24. return dp[i][j]=ways;
  25. }
  26.  
  27.  
  28. int Solution::numDecodings(string A) {
  29. //j = A.length()
  30. vector<vector<int>> dp(A.length()+1,vector<int>(A.length()+1,-1));
  31. int ans=f(0,A.length(),dp,A);
  32. if(ans<0) return 0;
  33. return ans;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement