<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>

class Solution:
    def maxLength(self, arr: List[str]) -> int:
        results = set([0])
        best = 0
        for word in arr:
            best = max(best, self.addWord(word, results))
        return best
    
    def addWord(self, word: str, results: List[str]) -> int:
        # Initialize an integer to represent the character set of the word.
        # char_bitset will use the lower 26 bits to track which characters (a-z) are present in the word.
        char_bitset = 0

        # Variable to track the best (longest) valid concatenation length found so far.
        best = 0

        # Iterate through each character in the word.
        for char in word:
            # Calculate the bitmask for the character, which shifts 1 to the left by (ASCII value of char - 'a').
            # This gives a unique bit position for each lowercase letter.
            mask = 1 << (ord(char) - 97)

            # Check if this character has already been seen in the word (i.e., the bit is already set in char_bitset).
            # If so, return 0 as this word contains duplicate characters, which disqualifies it.
            if char_bitset & mask > 0:
                return 0

            # Add the character to the char_bitset by setting the corresponding bit.
            char_bitset |= mask

        # If the current combination of char_bitset and word length already exists in results, return 0.
        # The bitshift of the word length ensures that we store both the length and the character set in a single value.
        if char_bitset + (len(word) << 26) in results:
            return 0

        # Iterate through all previous results (which are stored as combined bitsets and lengths).
        for res in list(results):
            # If the current word shares any characters with the result (i.e., there's a bit overlap), skip this result.
            # We check for overlap by performing a bitwise AND between char_bitset and the result's bitset.
            if res & char_bitset:
                continue

            # Calculate the new length of the concatenated string by adding the lengths of the current word and the result.
            # The length is stored in the upper bits of 'res', so we shift it right by 26 bits to extract it.
            new_res_len = (res >> 26) + len(word)

            # Combine the bitsets of the current word and the previous result.
            # The lower 26 bits of 'res' store the character bitset, so we mask out everything else.
            new_char_bitset = char_bitset + res & ((1 << 26) - 1)

            # Add the new combination of length and bitset to the results set.
            results.add((new_res_len << 26) + new_char_bitset)

            # Update the best (maximum) length found so far.
            best = max(best, new_res_len)

        # Return the best concatenation length found after processing the word.
        return best

20500