share my java solution!


  • 0
    T
    public class Solution {
        public int numDecodings(String s) {
            if(s==null || s.length()<1) return 0;
            char[] array=s.toCharArray();
            
            if(array[0]=='0'){
                return 0;
            }
            
            long[] res=new long[array.length+1];
            
            res[0] = 1;
            if(array[0] == '*'){
                res[1] = 9;
            }else{
                res[1] = 1;
            }
            
            long mod=(long)(1e9+7);
            for(int i=2; i <= array.length; i++){
                 if(array[i-1] == '*'){
                     res[i] = (9*res[i-1])%mod;
                     if(array[i-2] == '1'){
                         res[i] = (res[i]+(9*res[i-2]))%mod;
                     }else if(array[i-2]=='2'){
                         res[i] = (res[i]+(6*res[i-2]))%mod;
                     }else if(array[i-2]=='*'){
                         res[i]=(res[i]+(15*res[i-2]))%mod;
                     }
                 }else if(array[i-1] != '0'){
                     res[i] = res[i-1];
                     if(array[i-2] == '1'){
                         res[i] = (res[i]+res[i-2])%mod;
                     }else if(array[i-2] == '2'&&array[i-1]<='6'){
                         res[i]=(res[i]+res[i-2])%mod;
                     }else if(array[i-2] == '*'&&array[i-1]<='6'){
                         res[i]=(res[i]+res[i-2]*2)%mod;
                     }else if(array[i-2] == '*' && array[i-1]>'6'){
                         res[i]=(res[i]+res[i-2])%mod;
                     }
                 }else if(array[i-1] == '0'){
                     if(array[i-2]=='*'){
                         res[i]=(2*res[i-2])%mod;
                     }else if(array[i-2]=='1' || array[i-2]=='2'){
                         res[i]=res[i-2]%mod;
                     }else{
                         return 0;
                     }
                 }
            }
            
            return (int)res[res.length-1];
        }
    }
    
    

Log in to reply
 

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