GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — coding-problems/Dynamic Programming/number_of_decodings.py at master · mshakan/coding-problems

Expand file tree

/

number_of_decodings.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
102 lines (77 loc) · 2.1 KB
/

number_of_decodings.py

Copy path

File metadata and controls

102 lines (77 loc) · 2.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'''
Number of Decodings
Given the mapping a=1, b=2, ... , z=26, and an encoded message, count the number of ways it can be decoded.
For example, the message "111" would give 3, since it could be decoded as "aaa", "ka" and "ak".
All of the messages are decodable!
=========================================
The easiest solution is Brute-Force (building a tree and making all combinations),
and in the worst case there will be Fibbionaci(N) combinations, so the worst Time Complexity will be O(Fib(N))
Dynamic programming solution. Similar to number_of_smses.py.
Time Complexity: O(N)
Space Complexity: O(N)
'''
############
# Solution #
############
def num_decodings(code):
n = len(code)
dp = [0 for i in range(n)]
if n == 0:
return 0
dp[0] = 1
if n == 1:
return dp[0]
dp[1] = (code[1] != '0') + is_valid(code[0:2])
for i in range(2, n):
if code[i] != '0':
# looking for how many combinations are there till now if this is a single digit
dp[i] += dp[i-1]
if is_valid(code[i-1 : i+1]):
# looking for how many combinations are there till now if this is a number of 2 digits
dp[i] += dp[i-2]
return dp[n-1]
def is_valid(code):
k = int(code)
return (k < 27) and (k > 9)
###########
# Testing #
###########
# Test 1
# Correct result => 5
print(num_decodings('12151'))
# Test 2
# Correct result => 5
print(num_decodings('1111'))
# Test 3
# Correct result => 3
print(num_decodings('111'))
# Test 4
# Correct result => 1
print(num_decodings('1010'))
# Test 5
# Correct result => 4
print(num_decodings('2626'))
# Test 6
# Correct result => 1
print(num_decodings('1'))
# Test 7
# Correct result => 2
print(num_decodings('11'))
# Test 8
# Correct result => 3
print(num_decodings('111'))
# Test 9
# Correct result => 5
print(num_decodings('1111'))
# Test 10
# Correct result => 8
print(num_decodings('11111'))
# Test 11
# Correct result => 13
print(num_decodings('111111'))
# Test 12
# Correct result => 21
print(num_decodings('1111111'))
# Test 13
# Correct result => 34
print(num_decodings('11111111'))
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.