-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdditiveNumber2.py
More file actions
28 lines (26 loc) · 875 Bytes
/
AdditiveNumber2.py
File metadata and controls
28 lines (26 loc) · 875 Bytes
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
class Solution:
def isAdditiveNumber(self, num):
"""
:type num: str
:rtype: bool
"""
def isAdditive(num1, num2, index):
if index >= len(num):
return True
s = num1 + num2
if num[index: index + len(str(s))] == str(s):
return isAdditive(num2, s, index + len(str(s)))
return False
for i in range(len(num) - 2):
if i > 0 and num[0] == '0':
break
for j in range(i + 1, len(num) - 1):
if j > i + 1 and num[i + 1] == '0':
break
if isAdditive(int(num[:i + 1]), int(num[i + 1: j + 1]), j + 1):
return True
return False
if __name__ == '__main__':
num = input()
solution = Solution()
print(solution.isAdditiveNumber(num))