<p>You have <code>n</code>&nbsp;&nbsp;<code>tiles</code>, where each tile has one letter <code>tiles[i]</code> printed on it.</p>

<p>Return <em>the number of possible non-empty sequences of letters</em> you can make using the letters printed on those <code>tiles</code>.</p>

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

<pre>
<strong>Input:</strong> tiles = &quot;AAB&quot;
<strong>Output:</strong> 8
<strong>Explanation: </strong>The possible sequences are &quot;A&quot;, &quot;B&quot;, &quot;AA&quot;, &quot;AB&quot;, &quot;BA&quot;, &quot;AAB&quot;, &quot;ABA&quot;, &quot;BAA&quot;.
</pre>

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

<pre>
<strong>Input:</strong> tiles = &quot;AAABBC&quot;
<strong>Output:</strong> 188
</pre>

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

<pre>
<strong>Input:</strong> tiles = &quot;V&quot;
<strong>Output:</strong> 1
</pre>

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

<ul>
	<li><code>1 &lt;= tiles.length &lt;= 7</code></li>
	<li><code>tiles</code> consists of uppercase English letters.</li>
</ul>

class Solution:
    def numTilePossibilities(self, tiles: str) -> int:
        subsets, subset = [], []
        permutations, permutation = [], []
        len_tiles = len(tiles)
        
        def unique_subsets_dfs(index: str):
            if index == len_tiles:
                s_c = sorted(subset[:])
                if s_c not in subsets: subsets.append(s_c)
                return
            elif index > len_tiles: return

            subset.append(tiles[index])

            unique_subsets_dfs(index + 1)
            subset.pop()
            unique_subsets_dfs(index + 1)

        def unique_permutations_dfs(_subset: str, len_subset: int, d: Dict[str, int]):
            if len(permutation) == len_subset:
                permutations.append(permutation[:])
                return
            
            for i in range(len(_subset)):
                num = _subset[i]
                if permutation.count(num) < d[num] and _subset[i] != _subset[i - 1]:
                    permutation.append(num)
                    unique_permutations_dfs(_subset, len_subset, d)
                    permutation.pop()

        unique_subsets_dfs(0)
        subsets.pop()

        for _subset in subsets:
            d = {}
            for val in _subset:
                if val in d: d[val] += 1
                else: d[val] = 1
            unique_permutations_dfs(_subset, len(_subset), d)

        s_plus_p = permutations + subsets
        raw_sp, duplicate = [], []

        for sp in s_plus_p:
            if sp not in duplicate: 
                raw_sp.append(sp)
            duplicate.append(sp)

        return len(raw_sp)
23300