All problems

Problem 848

Number of Wonderful Substrings

Medium · 20 pts · String, Bit Manipulation, Prefix Sum, Hash Table

Problem

A string is wonderful if at most one letter appears an odd number of times in it. The word uses only the letters a to j. Count its non-empty wonderful substrings (counted by position). Keep a 10-bit mask of letter parities for every prefix. A substring is wonderful when the masks at its two ends are equal or differ in exactly one bit, so for each prefix add the counts of the same mask and of the 10 masks one bit away.

Input

A single string WORD made of the letters a to j.

Output

Print the number of wonderful substrings.

Constraints

1 <= |WORD| <= 100000

Example input

aba

Example output

4

Explanation

For aba, the substrings a, b, a and aba each have at most one letter with an odd count, while ab and ba have two. So the answer is 4.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Count Number of Homogenous Substrings Next: Arranging Coins