From 975c201dbd7bd60f3333d9cc82993146be062026 Mon Sep 17 00:00:00 2001 From: Nigesh Shakya Date: Fri, 9 Oct 2020 19:05:18 -0500 Subject: [PATCH] Added a leetcode solution for problem 0091 Decode Ways for hacktoberfest --- LeetCode/0091_Decode_Ways.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 LeetCode/0091_Decode_Ways.py diff --git a/LeetCode/0091_Decode_Ways.py b/LeetCode/0091_Decode_Ways.py new file mode 100644 index 0000000..e5d83ff --- /dev/null +++ b/LeetCode/0091_Decode_Ways.py @@ -0,0 +1,20 @@ +class Solution: + def numDecodings(self, s: str) -> int: + def in_range(n): + if n[0] == '0': + return False + to_int = int(n) + if to_int <= 26 and to_int > 0: + return True + return False + + N = len(s) + a, b = 1, 1 + if N == 0 or s[0] == '0': + return 0 + + for i in range(1, N): + extra = a if in_range(s[i-1:i+1]) else 0 + c = extra + (b if in_range(s[i]) else 0) + a, b = b, c + return b