PALSTR - Editorial

PROBLEM LINK:

Practice
Contest

Author: Sunny Aggarwal
Tester: Vasya Antoniuk
Editorialist: Pushkar Mishra

DIFFICULTY:

Medium-Hard

PREREQUISITES:

String Matching, Z-Algorithm, DP, Palindromes

PROBLEM:

Given three strings S_1, S_2 and S_3, find the number of palindromic strings such that they are formed by concatenation of a substring of S_1, a substring of S_2 and a substring of S_3.

EXPLANATION:

This problem is implementation heavy and requires certain reductions. Let’s try to simplify what the question asks for. We have to give the number of strings that we can form from substrings of S_1, S_2 and S_3 such that they are palindromic. To explain better, let x be a substring of S_1, y be a substring of S_2 and z be a substring of S_3. Then x+y+z should be palindromic (’+’ denotes concatenation operator).

To make the analysis of the problem a bit simpler, let’s divide the problem into following three cases:

  • Case 1: \mid x \mid = \mid z \mid, where \mid x \mid denotes length of x.
  • Case 2: \mid x \mid > \mid z \mid.
  • Case 3: \mid x \mid < \mid z \mid.

Case 1
What the first case tells us is that we take a substring from x from S_1, and equally sized substring z from S_3 and concatenate them with some substring y from S_2 in the order x+y+z. For x+y+z to be a palindrome, there there are certain conditions that need to be met. These are:

  • x = reverse(z).
  • y is a palindrome in itself.

How can we get the number of possible combinations of x, y and z that fall under this case? First, let us determine how many substrings y of S_2 are palindromic. This can be done in \mathcal{O}(N^2) using the standard DP approach used for finding the longest palindromic substring of a string. Once this is done, we need to calculate then number of substrings x of S_1 such that reverse(x) is a substring of S_3. To do this, we take the following steps:

  • We take a string S_3' = reverse(S_3).
  • Next, we consider all the suffixes of S_1.
  • For a particular suffix suf, we form a new string S_z = suf+\$+S_3' (’+’ denotes concatenation and $\
is a random character which doesn't appear in text). - We run the [Z-Algorithm][777] on $S_z$. The sum of values of the $Z$ array obtained from the running of this algorithm gives us the number of substrings $x$ of $S_1$ such that $reverse(x)$ = $z$. - Repeat for all suffixes of $S_1$. The running time of this part of the algorithm is also $\mathcal{O}(N^2)$ since we have to consider $N$ suffixes and for each one, the Z-Algorithm takes $\mathcal{O}(N)$ time. Once we have the count of required substrings $x$ of $S_1$, we can multiply this with the number of palindromic substrings $y$ of $S_2$ to get the number of strings $x+y+z$ that fall under this case. **Case 2 and 3** The key here is to reduce this case to Case 1. Let us assume without loss of generality that $\mid x \mid < \mid z \mid$. The other case is symmetrical. For $x+y+z$ to be a palindrome, the following conditions must hold: - $x$ = a prefix of $reverse(z)$. - ($y$ + remaining part of $z$) forms a palindrome. To make this clear, let's take a case. Let $x$ = $ab$, $y$ = $cda$ and $z$ = $dcba$. We see that $x$ is a prefix of $reverse(z)$. Also, ($y$ + remaining part of $z$), i.e., ($y$ + $dc$) = $cdadc$ forms a palindrome. Thus, $x+y+z$ = $abcdadcba$ is palindromic. How do we count all such $x+y+z$ that fall under this case? For this we first count the number of ways such that ($y$ + a substring of $p$ of $S_3$) form a palindrome (note we have taken variable $p$ here in order to avoid confusion with $z$ that we want in the final count). How do we go about it? For $p$, we find the number of substrings $y$ of $S_2$ such that $y$+$p$ is a palindrome. To do this, we take $S_3'$ = $reverse(S_3)$. For every suffix $suf$ of $S_2$, we run the Z-Algorithm on $suf+\$+S_3'$. Let us say that the Z value for some index $i$ after the $\

character is k. This means that p = S_3[i-k+1..i]. The number of required substrings y for this p is given by the number of palidromic substrings of suf such that they begin at index k+1 of the suf. How to do this?

This can be found from the same DP table as we built in Case 1. Once we have the number of y for a particular p which is equal to S_3[i-k+1..i], we store this value in an array temp\_count[i]. Basically, temp\_count[i] stores the number of palindromic strings y + p where p is a substring of S_3 that end at i^{th} position in S_3 and y is some substring of S_2. All of this is done in \mathcal{O}(N^2).

The remaning part is to calculate for each index i in S_3, the number of substrings x of S_1 such that x is also a substring of S_3 which begins at i. The count when multiplied with temp\_count[i] gives the number of palindromic strings x+y+z which fall under this case. The count can be easily calculated using Z-Algorithm in the same way as in Case 1. The complexity of this part is again \mathcal{O}(N^2). Case 3 is symmetrical to Case 2.

All in all, we have \mathcal{O}(N^2) algorithm for all cases. The overall complexity of the program is hence \mathcal{O}(N^2).

COMPLEXITY:

\mathcal{O}(N^2) per test case.

SAMPLE SOLUTIONS:

Author
Tester
Editorialist

1 Like

@pushkarmishra How are you including palindromes with odd length in the answer?

1 Like

Yes indeed

Can’t we just merge characters of all three strings and try yo find a palindrome string using characters of merged string. there might be some edge cases where this c program can break.

In case 2 : the number of palindromic strings y + p where p is a substring of S3 that start at ith position in S3’ has to be \sum_{r=1}^{k} sum of subs[r+1] if k is the z value at position i, instead of sumofsubs[k+1]. Because for every substring p that starts at i in s3’ and ends at i+r (r < k) we can take the prefix of corresponding suffix in s2 and add a palindrome that starts at r+1 in s2

NOTE : sumofsubs[r] denotes number of palindromic substrings starting at rth position in s2

I think we can first try all combinations of concatenating strings and the try to find palindrome strings of variable length. We will first try to find a palindrome string of length 2 and then increase the length of palindrome string by one in every iteration.