<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters</strong> in <code>s</code> which are not present in any of the substrings.</p>

<p>Return <em>the <strong>minimum</strong> number of extra characters left over if you break up </em><code>s</code><em> optimally.</em></p>

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

<pre>
<strong>Input:</strong> s = &quot;leetscode&quot;, dictionary = [&quot;leet&quot;,&quot;code&quot;,&quot;leetcode&quot;]
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can break s in two substrings: &quot;leet&quot; from index 0 to 3 and &quot;code&quot; from index 5 to 8. There is only 1 unused character (at index 4), so we return 1.

</pre>

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

<pre>
<strong>Input:</strong> s = &quot;sayhelloworld&quot;, dictionary = [&quot;hello&quot;,&quot;world&quot;]
<strong>Output:</strong> 3
<strong>Explanation:</strong> We can break s in two substrings: &quot;hello&quot; from index 3 to 7 and &quot;world&quot; from index 8 to 12. The characters at indices 0, 1, 2 are not used in any substring and thus are considered as extra characters. Hence, we return 3.
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 50</code></li>
	<li><code>1 &lt;= dictionary.length &lt;= 50</code></li>
	<li><code>1 &lt;= dictionary[i].length &lt;= 50</code></li>
	<li><code>dictionary[i]</code>&nbsp;and <code>s</code> consists of only lowercase English letters</li>
	<li><code>dictionary</code> contains distinct words</li>
</ul>

class TrieNode:
    def __init__(self):
        self.nodes = {}
        self.is_word = False

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

    def add(self, word):
        cur = self.root
        for w in word:
            if w not in cur.nodes:
                cur.nodes[w] = TrieNode()
            cur = cur.nodes[w]
        cur.is_word = True

class Solution:
    def minExtraChar(self, s: str, dictionary: List[str]) -> int:
        trie = Trie()
        for word in dictionary:
            trie.add(word)

        cache = {}

        def dfs(i):
            if i == len(s):
                return 0
            if i in cache:
                return cache[i]
            
            res = 1 + dfs(i + 1) # skip char

            cur = trie.root            
            for j in range(i, len(s)):
                if s[j] not in cur.nodes:
                    break
                cur = cur.nodes[s[j]]
                if cur.is_word:
                    res = min(res, dfs(j + 1))

            cache[i] = res
            return res

        return dfs(0)

# Time: O(n ^ n) = O(n ^ 2)
# Space: O(n)


    def minExtraChar2(self, s: str, dictionary: List[str]) -> int:
        words = set(dictionary)
        cache = {}

        def dfs(i):
            if i == len(s):
                return 0
            if i in cache:
                return cache[i]
            
            res = 1 + dfs(i + 1) # skip char
            
            for j in range(i, len(s)):
                if s[i: j + 1] in words:
                    res = min(res, dfs(j + 1))

            cache[i] = res
            return res

        return dfs(0)

# Time: O(n ^ n ^ n) = O(n ^ 3)
# Space: O(n)
    
    
    def minExtraChar_not(self, s: str, dictionary: List[str]) -> int:
        trie = Trie()
        for word in dictionary:
            trie.add(word)
        
        res = len(s)

        def backtrack(i, cur, cur_res):
            nonlocal res
            if i == len(s):
                res = min(res, cur_res)
                return

            if cur.is_word:
                backtrack(i, trie.root, cur_res)
            
            # don't take char
            backtrack(i + 1, cur, cur_res + 1)

            # take char
            if s[i] in cur.nodes:
                backtrack(i + 1, cur.nodes[s[i]], cur_res)
        
        
        backtrack(0, trie.root, 0)

        return res
21400