Skip to content

Commit 7ab0909

Browse files
Chris WuChris Wu
Chris Wu
authored and
Chris Wu
committed
no message
1 parent f1c7477 commit 7ab0909

File tree

2 files changed

+88
-24
lines changed

2 files changed

+88
-24
lines changed

problems/decode-string.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#recursion
2+
#the main idea is to only calculate the mosr outer layer. (which is when stack is empty.)
3+
#we put all the inner layer to another `decodeString()` to get its value.
4+
#time: O(N), N is the length of the string.
5+
#space: O(N).
6+
class Solution(object):
7+
def decodeString(self, s):
8+
opt = ''
9+
k = ''
10+
stack = []
11+
12+
for i, c in enumerate(s):
13+
if c=='[':
14+
stack.append(i)
15+
elif c==']':
16+
i_start = stack.pop()
17+
if not stack:
18+
opt += int(k)*self.decodeString(s[i_start+1:i])
19+
k = ''
20+
elif c.isdigit() and not stack:
21+
k+=c
22+
elif not stack:
23+
opt+=c
24+
return opt or s
25+
26+
#stack
27+
#as we iterate the string, we will dive into different level of brackets.
28+
#we store the value of the current level in the `opt`
29+
#and store the outer level in the stack
30+
#when we get out of this level, we calculate the value.
31+
#time: O(N), N is the length of the string.
32+
#space: O(N).
33+
class Solution(object):
34+
def decodeString(self, s):
35+
opt = ''
36+
stack = []
37+
k = ''
38+
39+
for c in s:
40+
if c=='[':
41+
stack.append(opt)
42+
stack.append(k)
43+
opt = ''
44+
k = ''
45+
elif c==']':
46+
n = stack.pop()
47+
pre = stack.pop()
48+
opt = pre+int(n)*opt
49+
elif c.isdigit():
50+
k+=c
51+
else:
52+
opt+=c
53+
return opt
54+
55+
56+
57+
58+
Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
class Solution(object):
22
def splitIntoFibonacci(self, S):
3-
def findIndex(s, num):
4-
for i, c in enumerate(num):
5-
if i>=len(s): break
6-
if c!=s[i]: break
7-
if i==len(num)-1: return i+1
8-
return -1
9-
10-
def search(fab, s):
11-
if len(s)==0: return fab
12-
if len(fab)<2: return []
13-
14-
target = fab[-1]+fab[-2]
15-
if target>2147483648: return []
16-
i = findIndex(s, str(target))
17-
if i>0: return search(fab+[target], s[i:])
18-
return []
19-
20-
for i in xrange(1, len(S)-2):
21-
for j in xrange(i+1, len(S)-1):
22-
#skip leading zero
23-
if (S[:i][0]!='0' and S[:i][0]=='0') or (S[i:j][0]!='0' and S[i:j][0]=='0'): continue
24-
opt = search([int(S[:i]), int(S[i:j])], S[j:])
25-
if len(opt)>0: return opt
26-
return []
3+
def is_bibonacci(opt, num):
4+
return num == opt[-1]+opt[-2]
5+
6+
def helper(s, first):
7+
if first==len(s) and len(opt)>=3:
8+
return True
9+
10+
for i in xrange(first, len(s)):
11+
if s[first]=='0' and i!=first: break #skip leading zero
12+
num = int(s[first:i+1])
13+
14+
if num>2147483648: break
15+
16+
#early termination
17+
if len(opt)>=2 and num>opt[-1]+opt[-2]:
18+
break
19+
20+
if len(opt)<=1 or is_bibonacci(opt, num):
21+
opt.append(int(num))
22+
if helper(s, i+1): return True
23+
opt.pop()
24+
return False
25+
26+
opt = []
27+
helper(S, 0)
28+
return opt
29+
30+
31+
32+
2733

2834

0 commit comments

Comments
 (0)