<p>You are given an array of strings <code>words</code> and a string <code>target</code>.</p>

<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> is a <span data-keyword="string-prefix">prefix</span> of <strong>any</strong> string in <code>words</code>.</p>

<p>Return the <strong>minimum</strong> number of <strong>valid</strong> strings that can be <em>concatenated</em> to form <code>target</code>. If it is <strong>not</strong> possible to form <code>target</code>, return <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">words = [&quot;abc&quot;,&quot;aaaaa&quot;,&quot;bcdef&quot;], target = &quot;aabcdabc&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>The target string can be formed by concatenating:</p>

<ul>
	<li>Prefix of length 2 of <code>words[1]</code>, i.e. <code>&quot;aa&quot;</code>.</li>
	<li>Prefix of length 3 of <code>words[2]</code>, i.e. <code>&quot;bcd&quot;</code>.</li>
	<li>Prefix of length 3 of <code>words[0]</code>, i.e. <code>&quot;abc&quot;</code>.</li>
</ul>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">words = [&quot;abababab&quot;,&quot;ab&quot;], target = &quot;ababaababa&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>The target string can be formed by concatenating:</p>

<ul>
	<li>Prefix of length 5 of <code>words[0]</code>, i.e. <code>&quot;ababa&quot;</code>.</li>
	<li>Prefix of length 5 of <code>words[0]</code>, i.e. <code>&quot;ababa&quot;</code>.</li>
</ul>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">words = [&quot;abcdef&quot;], target = &quot;xyz&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">-1</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= words.length &lt;= 100</code></li>
	<li><code>1 &lt;= words[i].length &lt;= 5 * 10<sup>4</sup></code></li>
	<li>The input is generated such that <code>sum(words[i].length) &lt;= 10<sup>5</sup></code>.</li>
	<li><code>words[i]</code> consists only of lowercase English letters.</li>
	<li><code>1 &lt;= target.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>target</code> consists only of lowercase English letters.</li>
</ul>

mod = 10**9 + 7
d = 111
mod1 = 10**9 + 9
d = 113

N = 10**5 + 10

def compute_prefix(prime,d):
    prefix = [1]
    for i in range(N+1):
        prefix.append((prefix[-1]*d)%prime)
    return prefix

prefix = compute_prefix(mod,111)
prefix1 = compute_prefix(mod1,113)


def compute(patterns,prime = 10**9 + 7, d = 111):
    seen = set()
    for word in patterns:
        hash_value = 0
        for char in word:
            hash_value = ((hash_value*d)%prime + ord(char) )%prime
            seen.add(hash_value)
    return seen


def compute_hash(target,prime,d):
    arr = [0]
    hash_value = 0
    for char in target:
        hash_value = ((hash_value*d)%prime + ord(char) )%prime
        arr.append(hash_value)
    return arr


class Solution:
    def minValidStrings(self, words: List[str], target: str) -> int:

        seen = compute(words,prime = 10**9 + 7,d=111)
        seen1 = compute(words,prime = 10**9 + 9,d=113)
        
        arr = compute_hash(target,prime=10**9 + 7,d=111)
        arr1 = compute_hash(target,prime=10**9 + 9,d=113)

        dp = [0]*len(target)
        for i in range(len(target)):
            l = i
            r = len(target)-1
            best_ans = 0
            while l <= r:
                mid = l + (r-l)//2
                if (arr[mid+1]-arr[i]*prefix[mid+1-i])%(mod) in seen and (arr1[mid+1]-arr1[i]*prefix1[mid+1-i])%mod1 in seen1:
                    best_ans = mid-i+1
                    l = mid+1
                else:
                    r = mid-1
            dp[i] = best_ans

        max_dist = 0
        curr_max_dist = 0
        ans = 0
        for i in range(len(dp)):
            if dp[i] > 0:
                max_dist = max(max_dist,i+dp[i])
            if curr_max_dist == i:
                curr_max_dist = max_dist
                ans += 1
            if curr_max_dist == i:
                return -1
        return ans
39568