<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>3</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>3</sup></code></li>
	<li><code>target</code> consists only of lowercase English letters.</li>
</ul>

class Node:
    def __init__(self, depth=0):
        self.children = {}
        self.failure = None
        self.is_end = False  # Indicates if a valid prefix ends at this node
        self.depth = depth   # Depth of the node (length of the prefix)

class ACAutomaton:
    def __init__(self):
        self.root = Node()

    def insert(self, word):
        node = self.root
        for c in word:
            if c not in node.children:
                # Increase depth by 1 from the parent node
                node.children[c] = Node(node.depth + 1)
            node = node.children[c]
            node.is_end = True  # Mark this node as an end of a valid prefix

    def build_failure_links(self):
        from collections import deque
        queue = deque()
        self.root.failure = self.root

        # Initialize failure links for the root's immediate children
        for child in self.root.children.values():
            child.failure = self.root
            queue.append(child)

        # Build failure links for the rest of the nodes
        while queue:
            current_node = queue.popleft()
            for c, child_node in current_node.children.items():
                failure_node = current_node.failure
                while failure_node != self.root and c not in failure_node.children:
                    failure_node = failure_node.failure
                child_node.failure = failure_node.children.get(c, self.root)
                queue.append(child_node)

class Solution:
    def minValidStrings(self, words, target):
        ac = ACAutomaton()

        # Insert all prefixes into the automaton
        for word in words:
            node = ac.root
            for c in word:
                if c not in node.children:
                    node.children[c] = Node(node.depth + 1)
                node = node.children[c]
                node.is_end = True  # Mark every node as an end of a valid prefix

        ac.build_failure_links()

        n = len(target)
        dp = [float('inf')] * (n + 1)
        dp[0] = 0  # Base case

        node = ac.root

        for i in range(n):
            c = target[i]
            # Traverse the automaton
            while node != ac.root and c not in node.children:
                node = node.failure
            node = node.children.get(c, ac.root)

            # Collect matches by following failure links
            temp_node = node
            while temp_node != ac.root:
                if temp_node.is_end:
                    length = temp_node.depth
                    if i + 1 >= length:
                        dp[i + 1] = min(dp[i + 1], dp[i + 1 - length] + 1)
                temp_node = temp_node.failure

            # Check if the root node itself is an end node (not needed here as root.depth = 0)

        return -1 if dp[n] == float('inf') else dp[n]
44414