-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3292-MinimumNumberOfValidStringsToFormTargetII.go
More file actions
104 lines (94 loc) · 3.2 KB
/
3292-MinimumNumberOfValidStringsToFormTargetII.go
File metadata and controls
104 lines (94 loc) · 3.2 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
// 3292. Minimum Number of Valid Strings to Form Target II
// You are given an array of strings words and a string target.
// A string x is called valid if x is a prefix of any string in words.
// Return the minimum number of valid strings that can be concatenated to form target.
// If it is not possible to form target, return -1.
// Example 1:
// Input: words = ["abc","aaaaa","bcdef"], target = "aabcdabc"
// Output: 3
// Explanation:
// The target string can be formed by concatenating:
// Prefix of length 2 of words[1], i.e. "aa".
// Prefix of length 3 of words[2], i.e. "bcd".
// Prefix of length 3 of words[0], i.e. "abc".
// Example 2:
// Input: words = ["abababab","ab"], target = "ababaababa"
// Output: 2
// Explanation:
// The target string can be formed by concatenating:
// Prefix of length 5 of words[0], i.e. "ababa".
// Prefix of length 5 of words[0], i.e. "ababa".
// Example 3:
// Input: words = ["abcdef"], target = "xyz"
// Output: -1
// Constraints:
// 1 <= words.length <= 100
// 1 <= words[i].length <= 5 * 10^4
// The input is generated such that sum(words[i].length) <= 10^5.
// words[i] consists only of lowercase English letters.
// 1 <= target.length <= 5 * 10^4
// target consists only of lowercase English letters.
import "fmt"
// KMP + 动态规划
func minValidStrings(words []string, target string) int {
helper := func(word, target string) []int {
s := word + "#" + target
n := len(s)
pi := make([]int, n)
for i := 1; i < n; i++ {
j := pi[i - 1]
for j > 0 && s[i] != s[j] {
j = pi[j - 1]
}
if s[i] == s[j] {
j++
}
pi[i] = j
}
return pi
}
n, inf := len(target), 1 << 31
back := make([]int, n)
max := func (x, y int) int { if x > y { return x; }; return y; }
for _, word := range words {
pi, m := helper(word, target), len(word)
for i := 0; i < n; i++ {
back[i] = max(back[i], pi[m + 1 + i])
}
}
dp := make([]int, n + 1)
for i := 1; i <= n; i++ {
dp[i] = inf
}
for i := 0; i < n; i++ {
dp[i + 1] = dp[i + 1 - back[i]] + 1
if dp[i + 1] > n {
return -1
}
}
return dp[n]
}
func main() {
// Example 1:
// Input: words = ["abc","aaaaa","bcdef"], target = "aabcdabc"
// Output: 3
// Explanation:
// The target string can be formed by concatenating:
// Prefix of length 2 of words[1], i.e. "aa".
// Prefix of length 3 of words[2], i.e. "bcd".
// Prefix of length 3 of words[0], i.e. "abc".
fmt.Println(minValidStrings([]string{"abc","aaaaa","bcdef"}, "aabcdabc")) // 3
// Example 2:
// Input: words = ["abababab","ab"], target = "ababaababa"
// Output: 2
// Explanation:
// The target string can be formed by concatenating:
// Prefix of length 5 of words[0], i.e. "ababa".
// Prefix of length 5 of words[0], i.e. "ababa".
fmt.Println(minValidStrings([]string{"abababab","ab"}, "ababaababa")) // 2
// Example 3:
// Input: words = ["abcdef"], target = "xyz"
// Output: -1
fmt.Println(minValidStrings([]string{"abcdef"}, "xyz")) // -1
}