Decode Ways


  • 0
    V

    public class Solution {
    public int numDecodings(String s)
    {
    if (s.equals(null) || s.equals("") )
    {
    return 0;
    }

    int N= s.length();
    int[] integerarray = new int[N + 1];
    integerarray[N] = 1;
    
    for (int i = N - 1; i >= 0; i--) 
    {
    	if (s.charAt(i) == '0')
        {
        integerarray[i] = 0;
        }
    	else if (i <= N - 2 && (s.charAt(i) == '1' || (s.charAt(i) == '2' && s.charAt(i + 1) <= '6')))
        {
    		 integerarray[i] =  integerarray[i + 1] +  integerarray[i + 2];
    	} 
        else
        {
    		 integerarray[i] =  integerarray[i + 1];
        }
    }
    return  integerarray[0];
    

    }
    }


Log in to reply
 

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