<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 {
public:
    void Try(int i, int from, int k, string tiles,string&s, set<string>&st)
    {
        for(int j=from; j< tiles.size(); j++)
        {
            
            s.push_back(tiles[j]);
            if(i==k)
            {
                st.insert(s);
            }
            else
            {
                Try(i+1,j+1, k, tiles, s, st);
            }
            s.pop_back();
            
            
        }
    }
    // sinh hoan vi
    void Try2(set<string>&st2, string a, string &permutation, vector<bool>& used)
    {
        if(permutation.size()==a.size())
        {
            st2.insert(permutation);
            return;
        }
        for(int i=0; i<a.size(); i++)
        {
            if(!used[i])
            {
                used[i]=true;
                permutation+=a[i];
                Try2(st2, a, permutation, used);
                used[i]=false;
                permutation.pop_back();
            }
        }
    }
    int numTilePossibilities(string tiles) {
        int ans=0;
        set<string> st;
        string s="";
        int n = tiles.size();
        // B1: sinh tap con
        for(int i=1; i<=n; i++)
        {
            Try(1,0, i, tiles, s, st);
        }
        // B2: sinh hoan vi tu cac tap con co 2 phan tu tro len
        set<string> st2;
        for(string a: st)
        {
            if(a.size()==1) ans++;
            else // doi voi cac phan tu co ky tu thi tim hoan vi
            {
                int strLen = a.size();
                string permutation = "";
                vector<bool> used(strLen, false);
                Try2(st2, a,permutation, used);
                
            }
        }
        // for(string a:st2)
        // {
        //     cout<<a<<' ';
        // }
        cout<<endl;
        ans+=st2.size();
        return ans;
    }
};
20268