5 line python


  • 0
    X

    Simple DP. Don't think I need to explain anything.

        def numDecodings(self, s):
            r = [1 for x in range(len(s) + 1)]
            for i in range(1, len(s) + 1):
                r[i] = r[i - 1] if s[i - 1] != "0" else 0
                r[i] += r[i - 2] if i - 2 >= 0 and int(s[i - 2:i]) > 9 and int(s[i - 2:i]) <= 26 else 0
            return r[-1] if len(r) > 1 else 0
    

Log in to reply
 

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