PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000151. Đếm xâu con kỳ diệu (Number of Wonderful Substrings)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm xâu con kỳ diệu (Number of Wonderful Substrings)

Number of Wonderful Substrings

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given a string ss over the letters a through j, count its nonempty contiguous substrings in which at most one distinct letter occurs an odd number of times. All other letters occur an even number of times, possibly zero. Count different pairs of endpoints separately even when their contents match.

Input

One line containing the string ss of length nn.

Output

Print the number of qualifying intervals as one integer.

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; n=∣s∣n=|s|; the characters of ss belong to abcdefghij.
  • Subtask 2 (30 points): 1≤n≤1000001\le n\le 100000; n=∣s∣n=|s|; the characters of ss belong to ab.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; n=∣s∣n=|s|; the characters of ss belong to abcdefghij.

Example 1

Input

aba

Output

4

Explanation

The three single-character intervals and [1,3] (aba) qualify. Both length-two intervals have two odd counts. The total is 4.

Example 2

Input

aabb

Output

9

Explanation

Four length-one, two length-two (aa, bb), two length-three (aab, abb), and the entire string qualify. Only [2,3] fails.

Example 3

Input

he

Output

2

Explanation

Both single-character intervals qualify; he has two odd counts.