QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 256 MB Total points: 10

#2113. Balanced words

Statistics

Balance is the most important thing everywhere. It is especially important when organizing programming contests, and we hope that the jury of this year's Potyczki Algorytmiczne is well aware of this.

We say that a word is balanced if every letter that appears in it appears the same number of times. For example, the words w, mama, potyczki, and aabbcbcccbaa are balanced, while the words oko, algorytmistrz, and abcba are not. Given a long word consisting only of the characters a, b, and c, count how many of its non-empty substrings (i.e., contiguous segments of letters) are balanced.

Note: Two identical words appearing as substrings at different positions are counted multiple times. For example, in the word oko, the balanced substrings are o, k, o, ok, and ko.

Input

The first line of input contains a non-empty word with a length not exceeding 300,000, consisting only of the characters a, b, and c.

Output

The output should contain a single integer representing the number of balanced substrings of the input word.

Examples

Input 1

aabbabcccba

Output 1

28

Note 1

The balanced substrings are: a, aa, aabb, aabbab, aabbabccc, ab, abba, abc, b, ba, bb, bc, c, cb, cba, cc, ccc. Note that some of them appear multiple times.

Subtasks

  • In some test groups, the length of the input word does not exceed 100.
  • In other test groups, the length of the input word does not exceed 3000.

In both of the above cases, there is at least one such group.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.