Under construction

Still being built. Things may change

Find the String

Time limit3sMemory limit1024 MB

Summary
Count substrings of T that have the same equality pattern as P, meaning they are a bijective relabeling of P's letters. Lengths are up to 1,000,000.
Level

Medium6 of 10

Topics
String matching, Hash map, Sliding window
Solved
No attempts yet

Problem

Two strings AA and BB made of lowercase English letters are called effectively equal if all of the following hold.

  1. AA and BB have the same length.
  2. For all integers ii and jj, if the ii-th and jj-th characters of AA are equal, then the ii-th and jj-th characters of BB are equal.
  3. For all integers ii and jj, if the ii-th and jj-th characters of AA are different, then the ii-th and jj-th characters of BB are different.

For example, A=A = aba and B=B = pqp are effectively equal. However, A=A = abca and B=B = abcb are not an effectively equal pair.

Given strings TT and PP, write a program that counts the contiguous substrings of TT that are effectively equal to PP.

For example, if T=T = abababbab and P=P = pqp, then the five substrings aba, bab, aba, bab, bab starting from the left of TT are effectively equal to PP.

You must write the following function.

  • int findP( char T [], char P [], int N, int M ): T is an array (string) of length N+1. P is an array of length M+1. T and P store lowercase strings of length N and M. The last position of T and P holds \0. findP must return the number of contiguous substrings of T that are effectively equal to P.

Constraints

1≤N≤1 000 0001 \le N \le 1\,000\,000, 1≤M≤N1 \le M \le N.

Examples1

  1. Example 1

    Input
    a
    a
    
    Expected output
    1