This page is still under construction.

Parts of this page are still being built. What you see may change.

Two Prefixes

Interview

Time limit1sMemory limit512 MB

Summary
Given strings s and t, count distinct strings formed by concatenating a non-empty prefix of s with a non-empty prefix of t.
Level

Medium6 of 10

Topics
String, String matching, Hash map, Prefix sum
Solved
No attempts yet

Problem

Misha once again did not do his math homework for today's lesson. As a punishment, his teacher Dr. Andrew decided to give him a hard but completely useless task.

Dr. Andrew wrote two strings ss and tt of lowercase English letters on the blackboard. He reminded Misha that a prefix of a string is a string formed by removing several (possibly none) of its last characters, and a concatenation of two strings is a string formed by appending the second string to the right of the first string.

The teacher asked Misha to write down on the blackboard all strings that are the concatenations of some non-empty prefix of ss and some non-empty prefix of tt. When Misha did it, Dr. Andrew asked him how many distinct strings are there. Misha spent almost the entire lesson doing that and completed the task.

Now he asks you to write a program that would do this task automatically.

Input

The first line contains the string ss consisting of lowercase English letters. The second line contains the string tt consisting of lowercase English letters.

The lengths of both string do not exceed 10510^5.

Output

Output a single integer, the number of distinct strings that are concatenations of some non-empty prefix of ss with some non-empty prefix of tt.

Hint

In the first example, the string ss has three non-empty prefixes: {a, ab, aba}. The string tt has two non-empty prefixes: {a, aa}. In total, Misha has written five distinct strings: {aa, aaa, aba, abaa, abaaa}. The string abaa has been written twice.

In the second example, Misha has written eight distinct strings: {aa, aaa, aaaa, aaaaa, aaaaaa, aaaaaaa, aaaaaaaa, aaaaaaaaa}.

Examples2

  1. Example 1

    Input
    aba
    aa
    
    Expected output
    5
    
  2. Example 2

    Input
    aaaaa
    aaaa
    
    Expected output
    8