-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1593-SplitAStringIntoTheMaxNumberOfUniqueSubstrings.go
More file actions
73 lines (63 loc) · 2.26 KB
/
1593-SplitAStringIntoTheMaxNumberOfUniqueSubstrings.go
File metadata and controls
73 lines (63 loc) · 2.26 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
// 1593. Split a String Into the Max Number of Unique Substrings
// Given a string s, return the maximum number of unique substrings that the given string can be split into.
// You can split string s into any list of non-empty substrings,
// where the concatenation of the substrings forms the original string.
// However, you must split the substrings such that all of them are unique.
// A substring is a contiguous sequence of characters within a string.
// Example 1:
// Input: s = "ababccc"
// Output: 5
// Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
// Example 2:
// Input: s = "aba"
// Output: 2
// Explanation: One way to split maximally is ['a', 'ba'].
// Example 3:
// Input: s = "aa"
// Output: 1
// Explanation: It is impossible to split the string any further.
// Constraints:
// 1 <= s.length <= 16
// s contains only lower case English letters.
import "fmt"
// Backtracking
func maxUniqueSplit(s string) int {
res, n := 0, len(s)
memo := make(map[string]bool)
var backtracking func(pos, curr int)
backtracking = func(pos, curr int) {
if pos >= n {
if curr > res {
res = curr
}
return
}
for i := pos + 1; i <= n; i++ {
ns := s[pos:i]
if v, ok := memo[ns]; ok && v { continue }
memo[ns] = true
backtracking(i, curr + 1)
memo[ns] = false
}
}
backtracking(0, 0)
return res
}
func main() {
// Example 1:
// Input: s = "ababccc"
// Output: 5
// Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
fmt.Println(maxUniqueSplit("ababccc")) // 5 ['a', 'b', 'a', 'b', 'c', 'cc']
// Example 2:
// Input: s = "aba"
// Output: 2
// Explanation: One way to split maximally is ['a', 'ba'].
fmt.Println(maxUniqueSplit("aba")) // 2 ['a', 'ba']
// Example 3:
// Input: s = "aa"
// Output: 1
// Explanation: It is impossible to split the string any further.
fmt.Println(maxUniqueSplit("aa")) // 1 ['aa']
}