<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 TrieNode:
    def __init__(self, ):
        self.child = [None] * 26
        self.isWord = False

class Trie:
    def __init__(self, ):
        self.root = TrieNode()

    def insert(self, word):
        p = self.root
        for ele in word:
            num = ord(ele) - ord('a')
            # print(ele)
            if p.child[num] == None:
                p.child[num] = TrieNode()
            p = p.child[num]
        p.isword = True
    
    def prefix(self, word):
        p = self.root
        for ele in word:
            num = ord(ele) - ord('a')
            if p.child[num] == None:
                return False
            p = p.child[num]
        return True

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

        # @cache
        # def check(m, n):
        #     string = target[m:n]
        #     # for word in wordset:
        #     for word in words:
        #         if len(string) <= len(word):
        #             if string == word[:len(string)]:
        #                 return True
        #     return False
        

        # @cache
        # def dfs(idx):
        #     if idx >= len(target):
        #         return 0
        #     answer = float('inf')
        #     for i in range(idx+1, len(target) + 1):
        #         # if check(target[idx:i]):
        #         if check(idx, i):
        #             answer = min(answer, 1 + dfs(i))
        #     return answer
        

        # out = dfs(0)
        
        # return out if out < float('inf') else -1


        # @cache
        # def check(m, n):
        #     string = target[m:n+1]
        #     # for word in wordset:
        #     for word in words:
        #         if len(string) <= len(word):
        #             if string == word[:len(string)]:
        #                 return True
        #     # for word in buc[len(string)]:
        #     #     if string == word:
        #     #         return True
        #     return False


        
        # # prefix = set()
        # # prefix = [[-1] * len(target) for _ in range(len(target))]
        # prefix = collections.defaultdict(set)
        # for i in range(len(target)):
            
        #     for j in range(i+1):
        #         if j == i:
        #             for num in range(len(words)):
        #                 if target[j:i+1] == words[num][0]:
        #                     prefix[(j,i)].add(num)
        #         # if prefix[j][i] != -1:
        #         # if prefix[(j, i)]
        #         else:
        #             if prefix[(j, i-1)]:
        #                 for idx in prefix[(j, i-1)]:
        #                     # print(idx, i, j)
        #                     if i - j < len(words[idx]) and target[i] == words[idx][i-j]:
        #                         prefix[(j, i)].add(idx)
        # # print(prefix)
        tr = Trie()
        for word in words:
            tr.insert(word)
        dp = [float('inf')] * (len(target) + 1)
        dp[0] = 0
        for i in range(len(target)):
            # if check(0, i):
            p = tr.root
            for j in range(i, len(target)):
                num = ord(target[j]) - ord('a')
                if p.child[num]:
                    # dp[j] = min(dp[j], dp[i-1] + 1)
                    dp[j+1] = min(dp[j+1], dp[i] + 1)
                    p = p.child[num]
                else:
                    break

            # if tr.prefix(target[:i+1]):
            #     dp[i] = 1
            #     # continue
            # else:
            #     for j in range(i):
            #         if dp[j] < float('inf'):
            #             # if check(j+1, i):
            #             # if prefix[(j+1, i)]:
            #             if tr.prefix(target[j+1:i+1]):
            #                 dp[i] = min(dp[i], dp[j] + 1)

        # # buc = collections.defaultdict(set)
        # # for word in words:
        # #     buc[len(word)].add(word)
        return dp[-1] if dp[-1] < float('inf') else -1

39526