Skip to content

Commit 0cd45c1

Browse files
committed
Time: 32 ms (77.78%), Space: 44.5 MB (92.16%) - LeetHub
1 parent 3cc5a98 commit 0cd45c1

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int maxUniqueSplit(String s) {
3+
int[] res = new int[1];
4+
Set<String> set = new HashSet<>();
5+
6+
dfs(res, s, set, 0);
7+
return res[0];
8+
}
9+
10+
private void dfs(int[] res, String s, Set<String> set, int cur) {
11+
if (cur == s.length()) {
12+
res[0] = Math.max(res[0], set.size());
13+
return;
14+
}
15+
16+
for (int i = cur; i < s.length(); i++) {
17+
String word = s.substring(cur, i + 1);
18+
if (!set.contains(word)) {
19+
set.add(word);
20+
dfs(res, s, set, i + 1);
21+
set.remove(word);
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)