All problems

Problem 720

Number of Substrings Containing All Three Characters

Medium · 20 pts · String, Sliding Window, Hash Table

Problem

A string consists only of the characters a, b and c. Count its substrings (blocks of consecutive characters, counted by position) that contain at least one a, at least one b and at least one c. For every end position, remember the last index where each of the three letters appeared; every start position up to the smallest of those three indices gives a valid substring, so add that index plus one.

Input

A single string S made of the letters a, b and c.

Output

Print the number of substrings that contain all three letters.

Constraints

3 <= |S| <= 100000

Example input

abcabc

Example output

10

Explanation

In abcabc, the substrings containing a, b and c are abc, abca, abcab, abcabc, bca, bcab, bcabc, cab, cabc and the last abc. That is 10 substrings.

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

Previous: Maximum Points You Can Obtain from Cards Next: Linked List Cycle