forked from shruti170901/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDecode Ways.cpp
More file actions
31 lines (28 loc) · 802 Bytes
/
Decode Ways.cpp
File metadata and controls
31 lines (28 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// https://leetcode.com/problems/decode-ways/
class Solution {
public:
vector<long long> memo;
long long num(string &s, long long idx){
if(idx==s.size()) return 1;
if(memo[idx]>=0) return memo[idx];
long long temp=0, t1=0;
if(s[idx]=='0'){
memo[idx]=0;
return 0;
}
temp=num(s, idx+1);
if(idx<s.size()-1){
long long x=(s[idx]-'0')*10+s[idx+1]-'0';
if(x>=10 && x<=26) temp+=num(s, idx+2);
//cout<<temp<<endl;
}
memo[idx]=temp;
return temp;
}
int numDecodings(string s) {
memo.resize(s.size(), -1);
long long ans=max(num(s, 0), 0ll);
for(auto it:memo) cout<<it<<" ";cout<<endl;
return ans;
}
};