golang solution


  • 0
    C
    import "math"
    
    func numDecodings(s string) int {
        res := make([]int, len(s))
        
        for i := 0; i < len(s); i++ {
            one, two := 0,0
            if s[i] == '*' {
                one = 9
            } else if s[i] > '0' {
                one = 1
            }
            
            if i == 0 {
                res[0] = one
                continue
            }
            
            if s[i-1] == '1' || s[i-1] == '*' {
                if s[i] == '*' {
                    two += 9
                } else {
                    two++
                }
            }      
            if s[i-1] == '2' || s[i-1] == '*' {
                if s[i] == '*' {
                    two += 6
                } else if s[i] < '7' {
                    two++
                }
            }
            one = one * res[i-1] % 1000000007
            if i > 1 {
                two = two * res[i-2] % 1000000007
            }
            res[i] = (one + two) % 1000000007
        }
        
        return res[len(s)-1]
    }
    

Log in to reply
 

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