#P11705. Isomorphic Substring Count

    ID: 13796 Type: Default 1000ms 256MiB

Isomorphic Substring Count

Isomorphic Substring Count

For two strings consisting of lowercase English letters, we say that the strings are actually the same (or isomorphic) if they satisfy the following conditions:

  1. The two strings have the same length.
  2. For all integers \(i\) and \(j\), if the \(i\)th and \(j\)th characters of the first string are equal, then the \(i\)th and \(j\)th characters of the second string are also equal.
  3. For all integers \(i\) and \(j\), if the \(i\)th and \(j\)th characters of the first string are different, then the \(i\)th and \(j\)th characters of the second string are also different.

For example, the strings \(A = a\,b\,a\) and \(B = p\,q\,p\) are isomorphic. However, \(A = a\,b\,c\,a\) and \(B = a\,b\,c\,b\) are not.

You are given two strings: a text string \(T\) and a pattern string \(P\). Your task is to count the number of contiguous substrings of \(T\) that are isomorphic to \(P\).

You need to implement the following function:

int findP(char T[], char P[], int N, int M);

Here:

  • T is a character array of length \(N+1\) where the first \(N\) characters form the string and the last character is the null terminator ('\0').
  • P is a character array of length \(M+1\) where the first \(M\) characters form the string and the last character is '\0'.
  • The function findP should return the count of contiguous substrings in \(T\) that are isomorphic to \(P\).

Note: Your submitted code should not perform any input/output operations or access any files.

inputFormat

The function findP receives the following parameters:

  • char T[]: A character array representing the text string of length \(N\) (plus a trailing '\0').
  • char P[]: A character array representing the pattern string of length \(M\) (plus a trailing '\0').
  • int N: The length of the text string \(T\).
  • int M: The length of the pattern string \(P\).

You do not need to worry about input parsing; just implement the function as specified.

outputFormat

The function should return an integer representing the number of contiguous substrings of \(T\) that are isomorphic to \(P\).

sample

T = "abababbab"\nP = "pqp"\nN = 9, M = 3
5