PROBLEM LINK:Author: Sunny Aggarwal DIFFICULTY:MediumHard PREREQUISITES:String Matching, ZAlgorithm, 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
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:
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 ZAlgorithm 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** For $x+y+z$ to be a palindrome, the following conditions must hold:
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 ZAlgorithm 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[ik+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[ik+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 ZAlgorithm 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:
This question is marked "community wiki".
asked 23 Jan '16, 00:19

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. answered 14 Sep '16, 17:23

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 answered 14 Nov '16, 02:49

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. answered 07 Apr '17, 11:46

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