File tree Expand file tree Collapse file tree 6 files changed +318
-2
lines changed
solution/1500-1599/1593.Split a String Into the Max Number of Unique Substrings Expand file tree Collapse file tree 6 files changed +318
-2
lines changed Original file line number Diff line number Diff line change 52
52
53
53
<!-- 这里可写通用的实现逻辑 -->
54
54
55
+ ** 方法一:DFS**
56
+
57
+ 经典 DFS 回溯问题。
58
+
55
59
<!-- tabs:start -->
56
60
57
61
### ** Python3**
58
62
59
63
<!-- 这里可写当前语言的特殊实现逻辑 -->
60
64
61
65
``` python
62
-
66
+ class Solution :
67
+ def maxUniqueSplit (self , s : str ) -> int :
68
+ def dfs (i , t ):
69
+ if i >= len (s):
70
+ nonlocal ans
71
+ ans = max (ans, t)
72
+ return
73
+ for j in range (i + 1 , len (s) + 1 ):
74
+ if s[i: j] not in vis:
75
+ vis.add(s[i: j])
76
+ dfs(j, t + 1 )
77
+ vis.remove(s[i: j])
78
+
79
+ vis = set ()
80
+ ans = 1
81
+ dfs(0 , 0 )
82
+ return ans
63
83
```
64
84
65
85
### ** Java**
66
86
67
87
<!-- 这里可写当前语言的特殊实现逻辑 -->
68
88
69
89
``` java
90
+ class Solution {
91
+ private Set<String > vis = new HashSet<> ();
92
+ private int ans = 1 ;
93
+ private String s;
94
+
95
+ public int maxUniqueSplit (String s ) {
96
+ this . s = s;
97
+ dfs(0 , 0 );
98
+ return ans;
99
+ }
100
+
101
+ private void dfs (int i , int t ) {
102
+ if (i >= s. length()) {
103
+ ans = Math . max(ans, t);
104
+ return ;
105
+ }
106
+ for (int j = i + 1 ; j <= s. length(); ++ j) {
107
+ String x = s. substring(i, j);
108
+ if (vis. add(x)) {
109
+ dfs(j, t + 1 );
110
+ vis. remove(x);
111
+ }
112
+ }
113
+ }
114
+ }
115
+ ```
116
+
117
+ ### ** C++**
118
+
119
+ ``` cpp
120
+ class Solution {
121
+ public:
122
+ unordered_set<string > vis;
123
+ string s;
124
+ int ans = 1;
125
+
126
+ int maxUniqueSplit(string s) {
127
+ this->s = s;
128
+ dfs(0, 0);
129
+ return ans;
130
+ }
131
+
132
+ void dfs (int i, int t) {
133
+ if (i >= s.size()) {
134
+ ans = max(ans, t);
135
+ return;
136
+ }
137
+ for (int j = i + 1; j <= s.size(); ++j) {
138
+ string x = s.substr(i, j - i);
139
+ if (!vis.count(x)) {
140
+ vis.insert(x);
141
+ dfs(j, t + 1);
142
+ vis.erase(x);
143
+ }
144
+ }
145
+ }
146
+ };
147
+ ```
70
148
149
+ ### **Go**
150
+
151
+ ```go
152
+ func maxUniqueSplit(s string) int {
153
+ ans := 1
154
+ vis := map[string]bool{}
155
+
156
+ var dfs func(i, t int)
157
+ dfs = func(i, t int) {
158
+ if i >= len(s) {
159
+ ans = max(ans, t)
160
+ return
161
+ }
162
+ for j := i + 1; j <= len(s); j++ {
163
+ x := s[i:j]
164
+ if !vis[x] {
165
+ vis[x] = true
166
+ dfs(j, t+1)
167
+ vis[x] = false
168
+ }
169
+ }
170
+ }
171
+ dfs(0, 0)
172
+ return ans
173
+ }
174
+
175
+ func max(a, b int) int {
176
+ if a > b {
177
+ return a
178
+ }
179
+ return b
180
+ }
71
181
```
72
182
73
183
### ** ...**
Original file line number Diff line number Diff line change 49
49
50
50
## Solutions
51
51
52
+ DFS.
53
+
52
54
<!-- tabs:start -->
53
55
54
56
### ** Python3**
55
57
56
58
``` python
57
-
59
+ class Solution :
60
+ def maxUniqueSplit (self , s : str ) -> int :
61
+ def dfs (i , t ):
62
+ if i >= len (s):
63
+ nonlocal ans
64
+ ans = max (ans, t)
65
+ return
66
+ for j in range (i + 1 , len (s) + 1 ):
67
+ if s[i: j] not in vis:
68
+ vis.add(s[i: j])
69
+ dfs(j, t + 1 )
70
+ vis.remove(s[i: j])
71
+
72
+ vis = set ()
73
+ ans = 1
74
+ dfs(0 , 0 )
75
+ return ans
58
76
```
59
77
60
78
### ** Java**
61
79
62
80
``` java
81
+ class Solution {
82
+ private Set<String > vis = new HashSet<> ();
83
+ private int ans = 1 ;
84
+ private String s;
85
+
86
+ public int maxUniqueSplit (String s ) {
87
+ this . s = s;
88
+ dfs(0 , 0 );
89
+ return ans;
90
+ }
91
+
92
+ private void dfs (int i , int t ) {
93
+ if (i >= s. length()) {
94
+ ans = Math . max(ans, t);
95
+ return ;
96
+ }
97
+ for (int j = i + 1 ; j <= s. length(); ++ j) {
98
+ String x = s. substring(i, j);
99
+ if (vis. add(x)) {
100
+ dfs(j, t + 1 );
101
+ vis. remove(x);
102
+ }
103
+ }
104
+ }
105
+ }
106
+ ```
107
+
108
+ ### ** C++**
109
+
110
+ ``` cpp
111
+ class Solution {
112
+ public:
113
+ unordered_set<string > vis;
114
+ string s;
115
+ int ans = 1;
116
+
117
+ int maxUniqueSplit(string s) {
118
+ this->s = s;
119
+ dfs(0, 0);
120
+ return ans;
121
+ }
122
+
123
+ void dfs (int i, int t) {
124
+ if (i >= s.size()) {
125
+ ans = max(ans, t);
126
+ return;
127
+ }
128
+ for (int j = i + 1; j <= s.size(); ++j) {
129
+ string x = s.substr(i, j - i);
130
+ if (!vis.count(x)) {
131
+ vis.insert(x);
132
+ dfs(j, t + 1);
133
+ vis.erase(x);
134
+ }
135
+ }
136
+ }
137
+ };
138
+ ```
63
139
140
+ ### **Go**
141
+
142
+ ```go
143
+ func maxUniqueSplit(s string) int {
144
+ ans := 1
145
+ vis := map[string]bool{}
146
+
147
+ var dfs func(i, t int)
148
+ dfs = func(i, t int) {
149
+ if i >= len(s) {
150
+ ans = max(ans, t)
151
+ return
152
+ }
153
+ for j := i + 1; j <= len(s); j++ {
154
+ x := s[i:j]
155
+ if !vis[x] {
156
+ vis[x] = true
157
+ dfs(j, t+1)
158
+ vis[x] = false
159
+ }
160
+ }
161
+ }
162
+ dfs(0, 0)
163
+ return ans
164
+ }
165
+
166
+ func max(a, b int) int {
167
+ if a > b {
168
+ return a
169
+ }
170
+ return b
171
+ }
64
172
```
65
173
66
174
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ unordered_set<string> vis;
4
+ string s;
5
+ int ans = 1 ;
6
+
7
+ int maxUniqueSplit (string s) {
8
+ this ->s = s;
9
+ dfs (0 , 0 );
10
+ return ans;
11
+ }
12
+
13
+ void dfs (int i, int t) {
14
+ if (i >= s.size ()) {
15
+ ans = max (ans, t);
16
+ return ;
17
+ }
18
+ for (int j = i + 1 ; j <= s.size (); ++j) {
19
+ string x = s.substr (i, j - i);
20
+ if (!vis.count (x)) {
21
+ vis.insert (x);
22
+ dfs (j, t + 1 );
23
+ vis.erase (x);
24
+ }
25
+ }
26
+ }
27
+ };
Original file line number Diff line number Diff line change
1
+ func maxUniqueSplit (s string ) int {
2
+ ans := 1
3
+ vis := map [string ]bool {}
4
+
5
+ var dfs func (i , t int )
6
+ dfs = func (i , t int ) {
7
+ if i >= len (s ) {
8
+ ans = max (ans , t )
9
+ return
10
+ }
11
+ for j := i + 1 ; j <= len (s ); j ++ {
12
+ x := s [i :j ]
13
+ if ! vis [x ] {
14
+ vis [x ] = true
15
+ dfs (j , t + 1 )
16
+ vis [x ] = false
17
+ }
18
+ }
19
+ }
20
+ dfs (0 , 0 )
21
+ return ans
22
+ }
23
+
24
+ func max (a , b int ) int {
25
+ if a > b {
26
+ return a
27
+ }
28
+ return b
29
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ private Set <String > vis = new HashSet <>();
3
+ private int ans = 1 ;
4
+ private String s ;
5
+
6
+ public int maxUniqueSplit (String s ) {
7
+ this .s = s ;
8
+ dfs (0 , 0 );
9
+ return ans ;
10
+ }
11
+
12
+ private void dfs (int i , int t ) {
13
+ if (i >= s .length ()) {
14
+ ans = Math .max (ans , t );
15
+ return ;
16
+ }
17
+ for (int j = i + 1 ; j <= s .length (); ++j ) {
18
+ String x = s .substring (i , j );
19
+ if (vis .add (x )) {
20
+ dfs (j , t + 1 );
21
+ vis .remove (x );
22
+ }
23
+ }
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def maxUniqueSplit (self , s : str ) -> int :
3
+ def dfs (i , t ):
4
+ if i >= len (s ):
5
+ nonlocal ans
6
+ ans = max (ans , t )
7
+ return
8
+ for j in range (i + 1 , len (s ) + 1 ):
9
+ if s [i : j ] not in vis :
10
+ vis .add (s [i : j ])
11
+ dfs (j , t + 1 )
12
+ vis .remove (s [i : j ])
13
+
14
+ vis = set ()
15
+ ans = 1
16
+ dfs (0 , 0 )
17
+ return ans
You can’t perform that action at this time.
0 commit comments