Count of palindromes that can be obtained by concatenating equal length prefix and substrings
Prerequisites: Z-algorithm
Given a string S, the task is to find the maximum number of palindromes that can be formed after performing the given steps:
- Choose a non-empty prefix P and a non-empty substring T of equal length.
- Reverse either P or T and concatenate them.
Note: P and T can be overlapping.
Examples:
Input: S = “abab”
Output: 6
Explanation:
Consider prefix as S[0 : 1] (= “ab“) and reverse the substring S[2 : 3]. Resultant string after concatenating prefix with reversed substring is “abba”, which is a palindrome.
All possible prefixes are [“a”, “ab”, ”aba”, ”abab”].
All possible suffixes are [“a”, ”b”, ”a”, ”b”, ”ab”, ”ba”, ”ab”, ”aba”, ”bab”, ”abab”].
Therefore, all possible palindromes are [“aa”, ”aa”, ”abba”, ”abba”, ”abaaba”, ”ababbaba”].
Input: S = “abcd”
Output: 4
Naive Approach: Generate all possible prefixes and all possible substrings. Concatenate all equal length prefix and substring(after reversing) pairs and check if the string obtained is palindrome or not.
Time Complexity: O(N3)
Auxiliary Space: O(N2)
Efficient Approach: The above approach can be optimized based on the observation that the length of the concatenated strings will always be even. Therefore, only those strings need to be considered in which the first half is the mirror image of the second half. Therefore, the problem reduces to counting substrings for every possible prefix which are equal to that prefix, which can be done using Z-function.
The z-function over a string will return an array Z[] where Z[i] represents the length of the longest prefix that same as the sub-string starting at position i and ending at position i+Z[i]. Hence, the count will be .
Follow the steps below to solve the problem:
- The idea is to maintain an interval [l, r] which is the interval with max r such that [l, r] is a prefix substring (substring which is also prefix).
- If i ≤ r, then Z[i] is equal to minimum of r – i + 1 and Z[i – 1].
- Now, increment Z[i] until i + Z[i] is less than N and character at index Z[i] and i + Z[i] are equal in the given string.
- Now, if i + Z[i] – 1 > r, then set l to i and r to i + Z[i] – 1.
- Repeat the above steps for i from 0 to N-1.
- The answer is the sum of Z[i] + 1 for each i (0 ≤ i ≤ N-1).
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int countPalindromes(string S)
{
int N = ( int )S.length();
vector< int > Z(N);
int l = 0, r = 0;
for ( int i = 1; i < N; i++) {
if (i <= r)
Z[i] = min(r - i + 1, Z[i - l]);
while (i + Z[i] < N
&& S[Z[i]] == S[i + Z[i]]) {
Z[i]++;
}
if (i + Z[i] - 1 > r) {
l = i;
r = i + Z[i] - 1;
}
}
int sum = 0;
for ( int i = 0; i < Z.size(); i++) {
sum += Z[i] + 1;
}
return sum;
}
int main()
{
string S = "abab" ;
cout << countPalindromes(S);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countPalindromes(String S)
{
int N = ( int )S.length();
int [] Z = new int [(N)];
int l = 0 , r = 0 ;
for ( int i = 1 ; i < N; i++)
{
if (i <= r)
Z[i] = Math.min(r - i + 1 ,
Z[i - l]);
while (i + Z[i] < N &&
S.charAt(Z[i]) ==
S.charAt(i + Z[i]))
{
Z[i]++;
}
if (i + Z[i] - 1 > r)
{
l = i;
r = i + Z[i] - 1 ;
}
}
int sum = 0 ;
for ( int i = 0 ; i < Z.length; i++)
{
sum += Z[i] + 1 ;
}
return sum;
}
public static void main (String[] args)
{
String S = "abab" ;
System.out.println(countPalindromes(S));
}
}
|
Python3
def countPalindrome(S):
N = len (S)
Z = [ 0 ] * N
l = 0
r = 0
for i in range ( 1 , N):
if i < = r:
Z[i] = min (r - i + 1 , Z[i - 1 ])
while ((i + Z[i]) < N and (S[Z[i]] = = S[i + Z[i]])):
Z[i] + = 1
if ((i + Z[i] - 1 ) > r):
l = ir = i + Z[i] - 1
sum = 0
for i in range ( 0 , len (Z)):
sum + = Z[i] + 1
return sum
S = "abab"
print (countPalindrome(S))
|
C#
using System;
public class GFG{
static int countPalindromes(String S)
{
int N = ( int )S.Length;
int [] Z = new int [(N)];
int l = 0, r = 0;
for ( int i = 1; i < N; i++)
{
if (i <= r)
Z[i] = Math.Min(r - i + 1,
Z[i - l]);
while (i + Z[i] < N &&
S[Z[i]] ==
S[i + Z[i]])
{
Z[i]++;
}
if (i + Z[i] - 1 > r)
{
l = i;
r = i + Z[i] - 1;
}
}
int sum = 0;
for ( int i = 0; i < Z.Length; i++)
{
sum += Z[i] + 1;
}
return sum;
}
public static void Main(String[] args)
{
String S = "abab" ;
Console.WriteLine(countPalindromes(S));
}
}
|
Javascript
<script>
function countPalindromes(S)
{
var N = S.length;
var Z = Array(N).fill(0);
var l = 0, r = 0;
for ( var i = 1; i < N; i++) {
if (i <= r)
Z[i] = Math.min(r - i + 1, Z[i - l]);
while (i + Z[i] < N
&& S[Z[i]] == S[i + Z[i]]) {
Z[i]++;
}
if (i + Z[i] - 1 > r) {
l = i;
r = i + Z[i] - 1;
}
}
var sum = 0;
for ( var i = 0; i < Z.length; i++) {
sum += Z[i] + 1;
}
return sum;
}
var S = "abab" ;
document.write( countPalindromes(S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
27 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...