<p>You are given an array of strings <code>arr</code>. A string <code>s</code> is formed by the <strong>concatenation</strong> of a <strong>subsequence</strong> of <code>arr</code> that has <strong>unique characters</strong>.</p>

<p>Return <em>the <strong>maximum</strong> possible length</em> of <code>s</code>.</p>

<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>

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

<pre>
<strong>Input:</strong> arr = [&quot;un&quot;,&quot;iq&quot;,&quot;ue&quot;]
<strong>Output:</strong> 4
<strong>Explanation:</strong> All the valid concatenations are:
- &quot;&quot;
- &quot;un&quot;
- &quot;iq&quot;
- &quot;ue&quot;
- &quot;uniq&quot; (&quot;un&quot; + &quot;iq&quot;)
- &quot;ique&quot; (&quot;iq&quot; + &quot;ue&quot;)
Maximum length is 4.
</pre>

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

<pre>
<strong>Input:</strong> arr = [&quot;cha&quot;,&quot;r&quot;,&quot;act&quot;,&quot;ers&quot;]
<strong>Output:</strong> 6
<strong>Explanation:</strong> Possible longest valid concatenations are &quot;chaers&quot; (&quot;cha&quot; + &quot;ers&quot;) and &quot;acters&quot; (&quot;act&quot; + &quot;ers&quot;).
</pre>

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

<pre>
<strong>Input:</strong> arr = [&quot;abcdefghijklmnopqrstuvwxyz&quot;]
<strong>Output:</strong> 26
<strong>Explanation:</strong> The only string in arr has all 26 characters.
</pre>

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

<ul>
	<li><code>1 &lt;= arr.length &lt;= 16</code></li>
	<li><code>1 &lt;= arr[i].length &lt;= 26</code></li>
	<li><code>arr[i]</code> contains only lowercase English letters.</li>
</ul>

// https://www.youtube.com/watch?v=d4SPuvkaeoo&ab_channel=NeetCode

class Solution {
public:    
    int maxLen = 0;
    
    bool overlap(set<char> charset, string s){
        for(char c : s){
            if(charset.find(c) != charset.end())
                return true;
            charset.insert(c);
        }
        return false;
    }
    
    void helper(vector<string>& arr, int stIdx, set<char>& charset){
        if(stIdx >= arr.size()){
            maxLen = max(maxLen, static_cast<int>(charset.size()));
            return;
        }
        
        if(!overlap(charset, arr[stIdx])){
            for(char c : arr[stIdx])
                charset.insert(c);
            helper(arr, stIdx + 1, charset);
            
            for(char c : arr[stIdx])
                charset.erase(c);
        }
        helper(arr, stIdx + 1, charset); // KEEP IT OUTSIDE. Since it will have to run even if above condition does not satisfy
        
        return;
    }
    
    int maxLength(vector<string>& arr) {
        set<char> charset;
        
        helper(arr, 0, charset);
        
        return maxLen;
    }
    
    
    
    // TLE
    
    /*
    vector<string> result;
    
    bool check(string s1, string s2){
        //if(s1.size() == 0 || s2.size() == 0)
        //    return true;
        
        unordered_set<char> uset;
        if(s1.size() != 0){
            for(char c : s1){
                if(uset.count(c) != 0)
                    return false;
                uset.insert(c);
            }
        }
        
        if(s2.size() != 0){
            for(char c : s2){
                if(uset.count(c) != 0)
                    return false;
                uset.insert(c);
            }
        }
        
        return true;
    }
    
    void helper_TLE(vector<string>& arr, int stIdx, string& res){
        int enIdx = arr.size() - 1;
        if(stIdx >= enIdx){
            result.push_back(res);
            maxLen = max(maxLen, (int)res.size());
            return;
        }
        
        for(int i = stIdx; i <= enIdx; i++){
            if(check(res, arr[i])){
                int n = res.size();
                
                res += arr[i];
                helper(arr, i + 1, res);
                
                res.erase(n);
                helper(arr, i + 1, res);
            }
        }
        
        return;
    }
    
    int maxLength_TLE(vector<string>& arr) {
        string res = "";
        helper_TLE(arr, 0, res);
        
        if(arr.size() == 1){
            if(check(arr[0], ""))
                return arr[0].size();
            else
                return 0;
        }
        
        vector<string> results = result;
        
        return maxLen;
    }
    */
};
282100