Decode Ways II


  • 0

    Click here to see the full article post


  • 0
    T

    When string is "0", it returns 1, should be 0. To fix it, second = s.charAt(0) == '*' ? 9 : s.charAt(0) == '0' ? 0 : 1;


  • 0

    @travellinglina Thanks for pointing it out. I have fixed the code.


  • 0
    A

    Example 2:
    Input: "1*"
    Output: 9 + 9 = 18

    This is wrong. "1*"'s '*' can vary from 0 to 9 to make "10"(J) to "19"(S) which is 10.
    J(10), K(11), L(12), M(13), N(14), O(15), P(16), Q(17), R(18), S(19), this is 10.

    So 9 + 10 = 19. Not 18, Please correct it. Thank you.


  • 0

    @Aeonaxx * rages from 1 to 9. It cant be 0.


  • 0
    T

    Hi, In the last lined in the below snippet, I believe it should be 17 instead of 15. Since, given that first char is * and second char is *, total no of 2 digit characters are 10,11...26 = 17 (K, L, M, N, O, ....Z)

    Please correct if wrong

    if (s.charAt(i) == '*') {
                    second = 9 * second;
                    if (s.charAt(i - 1) == '1')
                        second = (second + 9 * first) % M;
                    else if (s.charAt(i - 1) == '2')
                        second = (second + 6 * first) % M;
                    else if (s.charAt(i - 1) == '*')
                        second = (second + 15 * first) % M;
                }
    

  • 0
    K

    class Solution {
    public:
    int maxMod;
    int numDecodings(string s) {
    maxMod = 1000000007;
    //vector<int> dp(s.size(), -1);
    if (s.size() == 0)
    return 0;
    if (s.size() == 1)
    return getSValue(s[0]);
    if (s.size() == 2)
    return getValue(s[0], s[1]) + getSValue(s[0])getSValue(s[1]);
    long lastOne = getSValue(s[s.size()-1]);
    long lastTwo= getValue(s[s.size()-2], s[s.size()-1]) + getSValue(s[s.size()-2])getSValue(s[s.size()-1]);
    for (int i = s.size() - 3; i >= 0; i--)
    {
    long cur = ((getSValue(s[i])lastTwo) % maxMod + (getValue(s[i], s[i + 1])lastOne) % maxMod) % maxMod;
    lastOne = lastTwo;
    lastTwo = cur;
    }
    return lastTwo;
    }
    long getValue(char x, char y)
    {
    if (x == '0')
    return 0;
    if (x != '
    '&&y != '
    ')
    {
    if ((x - '0') * 10 + y - '0' <= 26)
    return 1;
    else
    return 0;
    }
    else
    {
    if (x != '
    ')
    {
    switch (x)
    {
    case '1':return 9;
    case '2':return 6;
    default:return 0;
    break;
    }
    }
    else
    {
    if (y != '
    ')
    {
    if (y - '0' < 7)
    return 2;
    else
    return 1;
    }
    else
    return 15;
    }
    }
    }
    long getSValue(char x)
    {
    if (x != ''&&x != '0')
    return 1;
    if (x == '
    ')
    return 9;
    return 0;
    }
    };


  • 0
    L

    there is an input case, '101010‘, it can be decoded as 'JJJ',so the answer is 1, but the " Custom Testcase " gives 0。


  • 0
    L

    and '20' has the same problem


Log in to reply
 

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