Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool isValid(string s) {
- if(s[0]=='0'){
- return false;
- }
- if(stoi(s)>26) {
- return false;
- }
- return true;
- }
- int f(string s, int currentSplit,vector<int> dp){
- if(dp[currentSplit]!=-1) {
- return dp[currentSplit];
- }
- if(currentSplit==0) { return 1;}
- if(currentSplit<0) { return 0;}
- int ans=0;
- if(currentSplit>=1 && isValid(s.substr(currentSplit-1,1))){
- ans+=f(s,currentSplit-1,dp);
- }
- if(currentSplit>=2 && isValid(s.substr(currentSplit-2,2))){
- ans+=f(s,currentSplit-2,dp);
- }
- return dp[currentSplit]=ans;
- }
- int numDecodings(string s) {
- vector<int>dp(s.length()+1,-1);
- return f(s,s.length(),dp);
- }
- };
- //at each position- we can try split or not split
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement