cswork / OAOpen practice workspace

SalesforceOA #1

String Subsequences

Estimated MediumJudge-ready

Topics (AI-labelled)SubsequencesPrefix & Ranges

Count how many times a pattern string of length exactly 3 occurs in the text as a subsequence. Occurrences are distinguished by the triple of kept positions; they need not be contiguous, and repeated letters are still counted by position.

Input

The first line is the pattern, and the second line is the text. The pattern has length 3 and the text has length 1..500000; both contain only A..Z.

Output

Output the exact integer, without taking a modulus, using a 64-bit integer.

Example 1

Input:

ABC
ABCBABC

Output:

5

Example 2

Input:

AAA
AAAA

Output:

4

Example 3

Input:

ABA
AB

Output:

0

Example notes

The three examples have 5, 4, and 0 choices of positions, respectively.

Editorial

Loading solution…