Posted on

Description

Submission

class Solution {
public:
    int numDecodings(string s) {
        if(s[0] == '0') return 0;
        vector<int> dp(s.size() + 1, 0);
        dp[0] = dp[1] = 1;
        for(int i = 2; i <= s.size(); ++i) {
            if(s[i-1] != '0')
                dp[i] = dp[i-1];
            if(s[i-2] == '1' || (s[i-2] == '2' && s[i-1] < '7')) {
                dp[i] += dp[i-2];
            }
        }
        return dp.back();
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *