<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 Solution:
#     def minValidStrings(self, words: List[str], target: str) -> int:
        
# class TrieNode:
#     def __init__(self):
#         self.children = {}
#         self.is_end = False

# class Trie:
#     def __init__(self):
#         self.root = TrieNode()
    
#     def insert(self, word):
#         node = self.root
#         for char in word:
#             if char not in node.children:
#                 node.children[char] = TrieNode()
#             node = node.children[char]
#         node.is_end = True

class Solution:
    def minValidStrings(self, words: List[str], target: str) -> int:
        trie = [[0] * 26]  
        is_word = [False]

        for word in words:
            node = 0
            for ch in word:
                idx = ord(ch) - ord('a')
                if trie[node][idx] == 0:
                    trie.append([0] * 26)
                    is_word.append(False)
                    trie[node][idx] = len(trie) - 1
                node = trie[node][idx]
                is_word[node] = True

        T = len(target)
        dp = [float('inf')] * (T + 1)
        dp[0] = 0

        for i in range(T):
            if dp[i] == float('inf'):
                continue
            node = 0
            j = i
            for j in range(i, T):
                idx = ord(target[j]) - ord('a')
                if trie[node][idx] == 0:
                    break
                node = trie[node][idx]
                j += 1
                if is_word[node]:
                    dp[j] = min(dp[j], dp[i] + 1)
        return dp[T] if dp[T] != float('inf') else -1
        
        
        
#         trie = Trie()
#         for word in words:
#             for i in range(len(word)):
#                 sub = word[:i+1][::-1]
#                 # print(sub)
#                 trie.insert(sub)
        
#         n = len(target)
#         dp = [float('inf')] * (n + 1)
#         dp[0] = 0
        
#         # target = target[::-1
        
#         for i in range(1, n + 1):
#             node = trie.root
#             for j in range(i, 0, -1):
#                 if i-j+1 > 5000: break
#                 # print(target[j-1], node.children)
#                 if target[j-1] not in node.children:
#                     break
#                 node = node.children[target[j-1]]
#                 if node.is_end:
#                     dp[i] = min(dp[i], dp[j-1] + 1)
#         # print(dp)
        
#         return dp[n] if dp[n] != float('inf') else -1
36594