Skip to content

Commit e8be0f5

Browse files
authored
Update 1593-split-a-string-into-the-max-number-of-unique-substrings.js
1 parent 6fc5dad commit e8be0f5

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

1593-split-a-string-into-the-max-number-of-unique-substrings.js

+30
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,33 @@ function bt(str, cur, idx, useds) {
1919
return ans
2020
}
2121
}
22+
23+
// another
24+
25+
/**
26+
* @param {string} s
27+
* @return {number}
28+
*/
29+
const maxUniqueSplit = function (s) {
30+
const N = s.length
31+
let ans = -1
32+
let curr = new Set()
33+
const backtrack = (pos) => {
34+
if (pos === N) {
35+
ans = Math.max(ans, curr.size)
36+
return
37+
}
38+
if (curr.size + (N - pos) <= ans) return
39+
for (let i = pos + 1; i <= N; i++) {
40+
const a = s.slice(pos, i)
41+
if (curr.has(a)) continue
42+
curr.add(a)
43+
backtrack(i)
44+
curr.delete(a)
45+
}
46+
}
47+
48+
backtrack(0)
49+
return ans
50+
}
51+

0 commit comments

Comments
 (0)