<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>
class Solution:
    def wonderfulSubstrings(self, word: str) -> int:
        # create array to keep track of how many times we have seen a given mask
        # 2^10 because we can have max 10 chars in a string, so if all chars are odd, then all bits are 1
        freq = [0] * 1024
        freq[0] = 1  # 0 will be string with no chars at all
        
        result = 0  # 0 wonderful substrings at start
        mask = 0    # no odd characters at starting
        for ch in word:
            mask ^= self.maskOf(ch)  # flip the bit corresponding to current char. If it was odd, it becomes even and so on
            
            # if we have seen this mask in past, then chars between that posn and current are all even
            # so it is a valid wonderful substring, hence we increase result
            # freq[mask] will be 0 if we have not seen this mask earlier
            result += freq[mask]
            
            # even count is taken care of. now we need to consider case where one char is odd
            # so for each char in current mask, we will flip it one by one, and check if we have seen the mask earlier
            # if we have seen it, then it means that the char we flipped will be odd, and others even
            for curr in range(ord('a'), ord('j')+1):
                maskToCheck = mask ^ self.maskOf(chr(curr))
                result += freq[maskToCheck]
            
            # now we have to update freq to say that we have seen the current mask
            freq[mask] += 1
        
        return result

    def maskOf(self, ch: str) -> int:
        # we start with a mask of 10 bits 0000000001
        # then we shift "1" as many times as the char position
        # so for 'a' we shift 0 times, for b shift once, and so on...
        # eg. final mask of "d" should be "0000001000"
        val = ord(ch) - ord('a')
        return 1 << val
2188