cswork / OAOpen practice workspace

Morgan StanleyOA #3

String Subsequences

Estimated MediumJudge-ready

Topics (AI-labelled)SubsequencesString DP

You are given strings s1 and s2, where s1 has length exactly 3. Count the ways to choose three strictly increasing indices i<j<k in s2 such that s2[i], s2[j], s2[k] equal the three characters of s1 in order. The characters do not need to be contiguous; different index choices are counted separately, even if they give the same characters. Output the exact integer without taking a modulus. The original starter declares int, but the full range overflows 32 bits; the Java/C++ solutions from the same fixed source use 64 bits. This site keeps the full range and explicitly requires exact integer output.

Input

The first line contains s1, of length exactly 3. The second line contains s2, with 1≤|s2|≤500000. Both lines contain only uppercase English letters A to Z, and repeated characters are allowed.

Output

Output the exact non-negative decimal number of ways, without a modulus or truncation. The result can be as large as 20833208333500000, which requires a 64-bit integer; floating-point approximations must not be used.

Example 1

Input:

HRW
HERHRWS

Output:

3

Example 2

Input:

ELO
HELLOWORLD

Output:

4

Example 3

Input:

ABC
ABCBABC

Output:

5

Example notes

All three public examples come from the original problem. HRW in HERHRWS matches the 1-based indices (1,3,6), (1,5,6), (4,5,6), for 3 ways; ELO in HELLOWORLD has 4 ways; ABC in ABCBABC has 5 ways.

Editorial

Loading solution…