-
Notifications
You must be signed in to change notification settings - Fork 0
/
#1593 maxUniqueSplit.py
48 lines (33 loc) · 968 Bytes
/
#1593 maxUniqueSplit.py
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
class Solution:
def maxUniqueSplit(self, s: str) -> int:
# WA
# seen = set()
# n = len(s)
# l = 0
# r = 1
# while r < n:
# subset = s[l:r]
# if subset not in seen:
# seen.add(subset)
# l = r
# r = l + 1
# else:
# r += 1
# if s[l:r] not in seen:
# seen.add(s[l:r])
# return len(seen)
n = len(s)
def solve(i, seen):
if i == n:
return 0
ans = 0
for j in range(i + 1, n + 1):
substr = s[i:j]
if substr not in seen:
# Backtrack sol
seen.add(substr)
# Can split on curr
ans = max(ans, 1 + solve(j, seen))
seen.remove(substr)
return ans
return solve(0, set())