<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>

class PolynomialRollingHash:
    M = 10**9 + 9
    P = 31  # 53, 29791, 11111, 111111
    P_INV = pow(P, M - 2, M)
    POWS = [1]

    def __init__(self, sequence=""):
        self.sequence = deque()
        self.hash = 0
        for element in sequence:
            self.append(element)

    def get_pow(self, i) -> int:
        while len(self.POWS) <= i:
            self.POWS.append(self.POWS[-1] * self.P % self.M)

        return self.POWS[i]

    def encode(self, element) -> int:
        """
        Encode a single element

        Note: avoid producing an encoding of 0.
        E.g. a -> 0 then the hashes for a, aa, aaa, ... are all 0.
        """

        # for str input
        return ord(element) - 96

    def append(self, element) -> int:
        x = self.encode(element)
        p = self.get_pow(len(self.sequence))
        self.sequence.append(element)

        self.hash = (self.hash + x * p) % self.M

        return self.hash

    def pop(self) -> int:
        element = self.sequence.pop()
        x = self.encode(element)
        p = self.get_pow(len(self.sequence))

        self.hash = (self.hash - x * p) % self.M

        return self.hash

    def appendleft(self, element) -> int:
        x = self.encode(element)
        self.sequence.appendleft(element)

        self.hash = (x + self.hash * self.P) % self.M

        return self.hash

    def popleft(self) -> int:
        element = self.sequence.popleft()
        x = self.encode(element)

        self.hash = ((self.hash - x) * self.P_INV) % self.M

        return self.hash


class Solution:
    def minValidStrings(self, words: List[str], target: str) -> int:
        p = 31
        M = 10**9 + 7
        p_inv = pow(p, M - 2, M)
        power = [1]
        for _ in range(10**5):
            power.append((power[-1] * p) % M)

        # compute the hash for all prefix
        prefixes = set()
        for w in words:
            # hasher = PolynomialRollingHash()
            h = 0
            for i, c in enumerate(w):
                # hasher.append(c)
                # prefixes.add(hasher.hash)
                h += (ord(c) - 96) * power[i]
                h %= M
                prefixes.add(h)
        
        n = len(target)
        dp = [inf] * n + [0] # 0 at the end as the sentinel value

        """
        Important observation:
        
        If longest match at dp[r] starts at l 
        then longest match at dp[r + 1] cannot starts before l

        Thus a sliding window is applicable here.
        """

        # hasher = PolynomialRollingHash()
        # h = hasher.append(target[0])
        h = (ord(target[0]) - 96) % M
        r = 0
        for l in range(n):
            # expand the window one step to the right
            while h in prefixes:
                dp[r] = dp[l - 1] + 1
                if r == n - 1:
                    return dp[r]
                r += 1
                # h = hasher.append(target[r])
                h += (ord(target[r]) - 96) * power[r - l]
                h %= M

            if l == r:
                return -1

            # h = hasher.popleft()
            h = (h - (ord(target[l]) - 96)) * p_inv
            h %= M
        
        return -1
27826