All problems

Problem 224

Count Pattern Occurrences

Medium · 20 pts · String, String Matching

Problem

You are given a text T and a pattern P. Count how many times P occurs in T as a contiguous substring. Occurrences may overlap: in "aaaa" the pattern "aa" starts at three different positions. The text and the pattern can each have up to a million characters, so checking every position character by character can be too slow in the worst case. Use a linear-time string matching algorithm such as KMP or the Z-function.

Input

Two lines: text T and pattern P (lowercase letters).

Output

Print the number of occurrences.

Constraints

1 <= |T|, |P| <= 1000000

Example input

aaaa
aa

Example output

3

Explanation

In "aaaa", the pattern "aa" starts at positions 0, 1 and 2 (the occurrences overlap). So the answer is 3.

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

Previous: Shortest Palindrome Next: Expand Run-Length Encoding