Re: [閒聊] 每日leetcode

作者: dont   2024-10-21 18:58:18
1593. Split a String Into the Max Number of Unique Substrings
## 思路
backtracking
用set紀錄s[:i]的字串組合
如果s[i:j]是新的組合就加到set 檢查s[j:]
## Code
```python
class Solution:
def maxUniqueSplit(self, s: str) -> int:
n = len(s)
seen = set()
self.res = 1
def backtrack(i):
if i == n:
self.res = max(self.res, len(seen))
return
for j in range(i+1, n+1):
if s[i:j] in seen:
continue
seen.add(s[i:j])
backtrack(j)
seen.remove(s[i:j])
backtrack(0)
return self.res
```

Links booklink

Contact Us: admin [ a t ] ucptt.com