Similar with "Climb stairs" but with more restrictions.

//    Ref: http://yucoding.blogspot.com/2013/01/leetcode-question-26-decode-ways.html
class Solution {
public:
    bool isValid(string s)
    {
        if (s[0] == '0') return false;
        if (s[0] > '2' || (s[0] == '2' && s[1] > '6')) return false;
        return true;
    }
    int numDecodings(string s) {
        if (s.length() == 0) return 0;
        if (s.length() == 1)
        {
            if (s[0] != '0') return 1;
            else return 0;
        }
        if (s[0] == '0') return 0;

        int *num = new int[s.length()];
        for (int i = 0; i < s.length(); i++)
            num[i] = 0;

        //    Initial
        num[0] = 1;        
        if (isValid(s.substr(0, 2)))    num[1] ++;
        if (s[0] != '0' && s[1] != '0') num[1] ++;        

        //    DP: num[i] = num[i-1] + num[i-2](if)
        for (int i = 2; i < s.length(); i++)
        {
            if (s[i] != '0') num[i] += num[i - 1];
            if (isValid(s.substr(i - 1, 2)))
                num[i] += num[i - 2];
        }

        int ret = num[s.length()-1];
        delete[] num;
        return ret;
    }
};