Count of three non-overlapping sub-strings which on concatenation forms a palindrome
Given a string str, the task is to count the number of ways a palindromic substring could be formed by the concatenation of three sub-strings x, y and z of the string str such that all of them are non-overlapping i.e. sub-string y occurs after substring x and sub-string z occurs after sub-string y.
Examples:
Input: str = “abca”
Output: 2
The two valid pairs are (“a”, “b”, “a”) and (“a”, “c”, “a”)
Input: str = “abba”
Output: 5
Approach: Find all the possible pairs of three non-overlapping sub-strings and for every pairs check whether the string generated by their concatenation is a palindrome or not. If yes then increment the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalin( int i, int j, int k, int l,
int p, int q, string s)
{
int start = i, end = q;
while (start < end) {
if (s[start] != s[end])
return false ;
start++;
if (start == j + 1)
start = k;
end--;
if (end == p - 1)
end = l;
}
return true ;
}
int countSubStr(string s)
{
int count = 0;
int n = s.size();
for ( int i = 0; i < n - 2; i++) {
for ( int j = i; j < n - 2; j++) {
for ( int k = j + 1; k < n - 1; k++) {
for ( int l = k; l < n - 1; l++) {
for ( int p = l + 1; p < n; p++) {
for ( int q = p; q < n; q++) {
if (isPalin(i, j, k, l, p, q, s)) {
count++;
}
}
}
}
}
}
}
return count;
}
int main()
{
string s = "abca" ;
cout << countSubStr(s);
return 0;
}
|
Java
class GFG
{
static boolean isPalin( int i, int j, int k, int l,
int p, int q, String s)
{
int start = i, end = q;
while (start < end) {
if (s.charAt(start) != s.charAt(end))
{
return false ;
}
start++;
if (start == j + 1 )
{
start = k;
}
end--;
if (end == p - 1 )
{
end = l;
}
}
return true ;
}
static int countSubStr(String s)
{
int count = 0 ;
int n = s.length();
for ( int i = 0 ; i < n - 2 ; i++)
{
for ( int j = i; j < n - 2 ; j++)
{
for ( int k = j + 1 ; k < n - 1 ; k++)
{
for ( int l = k; l < n - 1 ; l++)
{
for ( int p = l + 1 ; p < n; p++)
{
for ( int q = p; q < n; q++)
{
if (isPalin(i, j, k, l, p, q, s))
{
count++;
}
}
}
}
}
}
}
return count;
}
public static void main(String[] args)
{
String s = "abca" ;
System.out.println(countSubStr(s));
}
}
|
Python3
def isPalin(i, j, k, l, p, q, s) :
start = i; end = q;
while (start < end) :
if (s[start] ! = s[end]) :
return False ;
start + = 1 ;
if (start = = j + 1 ) :
start = k;
end - = 1 ;
if (end = = p - 1 ) :
end = l;
return True ;
def countSubStr(s) :
count = 0 ;
n = len (s);
for i in range (n - 2 ) :
for j in range (i, n - 2 ) :
for k in range (j + 1 , n - 1 ) :
for l in range (k, n - 1 ) :
for p in range (l + 1 , n) :
for q in range (p, n) :
if (isPalin(i, j, k, l, p, q, s)) :
count + = 1 ;
return count;
if __name__ = = "__main__" :
s = "abca" ;
print (countSubStr(s));
|
C#
using System;
class GFG
{
static bool isPalin( int i, int j, int k, int l,
int p, int q, String s)
{
int start = i, end = q;
while (start < end)
{
if (s[start] != s[end])
{
return false ;
}
start++;
if (start == j + 1)
{
start = k;
}
end--;
if (end == p - 1)
{
end = l;
}
}
return true ;
}
static int countSubStr(String s)
{
int count = 0;
int n = s.Length;
for ( int i = 0; i < n - 2; i++)
{
for ( int j = i; j < n - 2; j++)
{
for ( int k = j + 1; k < n - 1; k++)
{
for ( int l = k; l < n - 1; l++)
{
for ( int p = l + 1; p < n; p++)
{
for ( int q = p; q < n; q++)
{
if (isPalin(i, j, k, l, p, q, s))
{
count++;
}
}
}
}
}
}
}
return count;
}
public static void Main(String[] args)
{
String s = "abca" ;
Console.WriteLine(countSubStr(s));
}
}
|
Javascript
<script>
function isPalin(i, j, k, l, p, q, s)
{
var start = i, end = q;
while (start < end) {
if (s[start] != s[end])
return false ;
start++;
if (start == j + 1)
start = k;
end--;
if (end == p - 1)
end = l;
}
return true ;
}
function countSubStr(s)
{
var count = 0;
var n = s.length;
for ( var i = 0; i < n - 2; i++) {
for ( var j = i; j < n - 2; j++) {
for ( var k = j + 1; k < n - 1; k++) {
for ( var l = k; l < n - 1; l++) {
for ( var p = l + 1; p < n; p++) {
for ( var q = p; q < n; q++) {
if (isPalin(i, j, k, l, p, q, s)) {
count++;
}
}
}
}
}
}
}
return count;
}
var s = "abca" ;
document.write( countSubStr(s));
</script>
|
Time Complexity: O(n7), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
21 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...