Skip to content

Commit 7180a8f

Browse files
author
Partho Biswas
committed
numbers in PI
1 parent cca8434 commit 7180a8f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

algoexpert.io/python/Numbers_In_Pi.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
3+
# O(n^3) time | O(n + m) space
4+
def numbersInPi(pi, numbers):
5+
numbersTable = {number: True for number in numbers}
6+
minSpaces = getMinSpaces(pi, numbersTable, {}, 0)
7+
return -1 if minSpaces == float('inf') else minSpaces
8+
9+
10+
def getMinSpaces(pi, numbersTable, cache, idx):
11+
if idx == len(pi):
12+
return -1
13+
if idx in cache:
14+
return cache[idx]
15+
minSpaces = float('inf')
16+
for i in range(idx, len(pi)):
17+
prefix = pi[idx:i + 1]
18+
if prefix in numbersTable:
19+
minSpacesInSiffix = getMinSpaces(pi, numbersTable, cache, i + 1)
20+
minSpaces = min(minSpaces, minSpacesInSiffix + 1)
21+
cache[idx] = minSpaces
22+
return cache[idx]
23+
24+

0 commit comments

Comments
 (0)