<p>A <strong>wonderful</strong> string is a string where <strong>at most one</strong> letter appears an <strong>odd</strong> number of times.</p>

<ul>
	<li>For example, <code>&quot;ccjjc&quot;</code> and <code>&quot;abab&quot;</code> are wonderful, but <code>&quot;ab&quot;</code> is not.</li>
</ul>

<p>Given a string <code>word</code> that consists of the first ten lowercase English letters (<code>&#39;a&#39;</code> through <code>&#39;j&#39;</code>), return <em>the <strong>number of wonderful non-empty substrings</strong> in </em><code>word</code><em>. If the same substring appears multiple times in </em><code>word</code><em>, then count <strong>each occurrence</strong> separately.</em></p>

<p>A <strong>substring</strong> is a contiguous sequence of characters in a string.</p>

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

<pre>
<strong>Input:</strong> word = &quot;aba&quot;
<strong>Output:</strong> 4
<strong>Explanation:</strong> The four wonderful substrings are underlined below:
- &quot;<u><strong>a</strong></u>ba&quot; -&gt; &quot;a&quot;
- &quot;a<u><strong>b</strong></u>a&quot; -&gt; &quot;b&quot;
- &quot;ab<u><strong>a</strong></u>&quot; -&gt; &quot;a&quot;
- &quot;<u><strong>aba</strong></u>&quot; -&gt; &quot;aba&quot;
</pre>

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

<pre>
<strong>Input:</strong> word = &quot;aabb&quot;
<strong>Output:</strong> 9
<strong>Explanation:</strong> The nine wonderful substrings are underlined below:
- &quot;<strong><u>a</u></strong>abb&quot; -&gt; &quot;a&quot;
- &quot;<u><strong>aa</strong></u>bb&quot; -&gt; &quot;aa&quot;
- &quot;<u><strong>aab</strong></u>b&quot; -&gt; &quot;aab&quot;
- &quot;<u><strong>aabb</strong></u>&quot; -&gt; &quot;aabb&quot;
- &quot;a<u><strong>a</strong></u>bb&quot; -&gt; &quot;a&quot;
- &quot;a<u><strong>abb</strong></u>&quot; -&gt; &quot;abb&quot;
- &quot;aa<u><strong>b</strong></u>b&quot; -&gt; &quot;b&quot;
- &quot;aa<u><strong>bb</strong></u>&quot; -&gt; &quot;bb&quot;
- &quot;aab<u><strong>b</strong></u>&quot; -&gt; &quot;b&quot;
</pre>

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

<pre>
<strong>Input:</strong> word = &quot;he&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> The two wonderful substrings are underlined below:
- &quot;<b><u>h</u></b>e&quot; -&gt; &quot;h&quot;
- &quot;h<strong><u>e</u></strong>&quot; -&gt; &quot;e&quot;
</pre>

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

<ul>
	<li><code>1 &lt;= word.length &lt;= 10<sup>5</sup></code></li>
	<li><code>word</code> consists of lowercase English letters from <code>&#39;a&#39;</code>&nbsp;to <code>&#39;j&#39;</code>.</li>
</ul>
import collections
class Solution:
    def wonderfulSubstrings(self, word: str) -> int:
        mask = 0
        premask = []
        seen = collections.defaultdict(int)
        count = 0
        seen[0] = 1
        for i, w in enumerate(word):
            string = 1 << int(ord(w) - ord("a")) 
            mask = mask ^ string
            
            if mask in seen:
                count += seen[mask]
            seen[mask] += 1

            for j in range(26):
                newmask = 1 << j
                if mask ^ newmask in seen:
                    count += seen[mask^newmask]
        return count

        count = 0
        seen = collections.defaultdict(int)
        
        # print(premask)
        for i in range(len(word)):
            value = sum([int(v) for v in premask[i]])
            if value <= 1:
                count += 1
                count += seen[premask[i]]
            if premask[i][1:] in seen:
                # print("strip: ", premask[i][1:], i)
                count += seen[premask[i][1:]]
            if "1" + premask[i] in seen:
                count += seen["1" + premask[i]]
            for j, char in enumerate(premask[i]):
                if char == "0":
                    newchar = "1"
                else: newchar = "0"
                # newchar = "1" if char == "0" else "0"
                newstring = premask[i][:j] + newchar + premask[i][j+1:]
                if newstring in seen:
                    # print(newstring, i)
                    count += seen[newstring]
            seen[premask[i]] += 1
            # for j in range(i+1, len(word)):
            #     string1 = premask[i]
            #     string2 = premask[j]
            #     val = int(string1, 2) ^ int(string2, 2)
            #     newstring = list(bin(val)[2:])
            #     if sum([int(v) for v in newstring]) <= 1:
            #         count += 1
        print(seen)
        return count
2504