Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int f(int i,int j,vector<vector<int>>&dp,string &s) {
- if(i>j) return 0;
- if(dp[i][j]!=-1) return dp[i][j];
- int ways=0;
- int count=0;
- for(int ind=i;ind<min(i+2,j);ind++) {
- if(ind==i) {
- if(s[i]=='0') continue;
- }
- if(ind==i+1) {
- int no = (s[i]-'0')*10+(s[i]-'0');
- if(no>26) continue;
- if(no<10) continue;
- if(s[i]=='0') continue;
- }
- ways += 1 + f(ind+1,j,dp,s);
- count+=1;
- }
- if(count==0) {
- return 0;
- exit(0);
- }
- return dp[i][j]=ways;
- }
- int Solution::numDecodings(string A) {
- //j = A.length()
- vector<vector<int>> dp(A.length()+1,vector<int>(A.length()+1,-1));
- int ans=f(0,A.length(),dp,A);
- if(ans<0) return 0;
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement