A concise c++ solution


  • 1
    5
    class Solution {
    public:
        int numDecodings(string s) {
            if(s.size() == 0 || s[0] == '0') return 0;
            vector<int> dp(s.size()+1, 0);
            dp[1] = dp[0] = 1;
            for(auto i = 2; i<= s.size(); i++)
            {
                if(s[i-1] >'0' && s[i-1] <='9')
                    dp[i] += dp[i-1];
                if(s.substr(i-2,2) >="10" && s.substr(i-2,2) <= "26")
                    dp[i] += dp[i-2];
            }
            return dp[s.size()];
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.