Minimum Number of Valid Strings to Form Target II Problem
Description
LeetCode Problem 3292.
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 <= 1001 <= 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^4targetconsists only of lowercase English letters.
Solution
Approach: Hashing + Binary Search + Greedy Jumps
Here target can have 5 * 10^4 characters, so the quadratic search no longer fits. Valid strings are the prefixes of the words, and the set of prefixes of each length can be stored as hashes: s[len] holds the hashes of all prefixes of length len.
For each position i, let f(i) be the length of the longest valid string that starts at target[i]. If a prefix of length L matches at i, so does every shorter one, so f(i) can be found by binary search over L, comparing the rolling hash of target[i..i+L-1] against s[L].
Now the problem is a "jump game": from position i we can reach any position up to i + f(i). Greedily scan the target, tracking the farthest reach mx; whenever we reach the end of the current piece (i == last), start a new piece, which counts one more string. If at that moment mx == i, nothing can cover target[i], so return -1.
Complexity:
- Time complexity:
O(L + n log m), wheremis the longest word length - Space complexity:
O(L + n)
class Hashing:
__slots__ = ["mod", "h", "p"]
def __init__(self, s: List[str], base: int, mod: int):
self.mod = mod
self.h = [0] * (len(s) + 1)
self.p = [1] * (len(s) + 1)
for i in range(1, len(s) + 1):
self.h[i] = (self.h[i - 1] * base + ord(s[i - 1])) % mod
self.p[i] = (self.p[i - 1] * base) % mod
def query(self, l: int, r: int) -> int:
return (self.h[r] - self.h[l - 1] * self.p[r - l + 1]) % self.mod
class Solution:
def minValidStrings(self, words: List[str], target: str) -> int:
def f(i: int) -> int:
l, r = 0, min(n - i, m)
while l < r:
mid = (l + r + 1) >> 1
sub = hashing.query(i + 1, i + mid)
if sub in s[mid]:
l = mid
else:
r = mid - 1
return l
base, mod = 13331, 998244353
hashing = Hashing(target, base, mod)
m = max(len(w) for w in words)
s = [set() for _ in range(m + 1)]
for w in words:
h = 0
for j, c in enumerate(w, 1):
h = (h * base + ord(c)) % mod
s[j].add(h)
ans = last = mx = 0
n = len(target)
for i in range(n):
dist = f(i)
mx = max(mx, i + dist)
if i == last:
if i == mx:
return -1
last = mx
ans += 1
return ans