Count pair of strings whose concatenation of substrings form a palindrome
Given an array of strings arr[], the task is to count the pair of strings whose concatenation of substrings form a palindrome.
Examples:
Input: arr[] = {“gfg”, “gfg”}
Output: 1
Explanation:
One possible way of choosing s1 and s2 is s1 = “gf”, s2 = “g” such that s1 + s2 i.e “gfg” is a palindrome.
Input: arr[] = {“abc”, B = “def”}
Output: 0
Approach: The key observation in the problem is if both strings have at least one common character let’s say ‘c’ then we can form a palindromic string. Therefore, check for all the pairs in the array that there is a common character in the string or not.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
bool isPossible(string A, string B)
{
sort(B.begin(),B.end());
int c=0;
for ( int i = 0; i < ( int )A.size(); i++)
if (binary_search(B.begin(),B.end(),A[i]))
return true ;
return false ;
}
int countPalindromicSubstrings(string s[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++){
for ( int j = i + 1; j < n; j++)
if (isPossible(s[i], s[j]))
count++;
}
return count;
}
int main()
{
string arr[] = { "gfg" , "gfg" };
int n = 2;
cout << countPalindromicSubstrings(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isPossible(String A, String B)
{
B = sortString(B);
for ( int i = 0 ; i < ( int )A.length(); i++)
if (Arrays.binarySearch(B.toCharArray(),
A.charAt(i)) > - 1 )
return true ;
return false ;
}
static int countPalindromicSubStrings(String s[],
int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i + 1 ; j < n; j++)
if (isPossible(s[i], s[j]))
count++;
}
return count;
}
static String sortString(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
public static void main(String[] args)
{
String arr[] = { "gfg" , "gfg" };
int n = 2 ;
System.out.print(countPalindromicSubStrings(arr, n));
}
}
|
Python3
def isPossible(A, B):
B = sorted (B)
c = 0
for i in range ( len (A)):
if A[i] in B:
return True
return False
def countPalindromicSubstrings(s, n):
count = 0
for i in range (n):
for j in range (i + 1 , n):
if (isPossible(s[i], s[j])):
count + = 1
return count
arr = [ "gfg" , "gfg" ]
n = 2
print (countPalindromicSubstrings(arr, n))
|
C#
using System;
class GFG{
static bool isPossible(String A, String B)
{
B = sortString(B);
for ( int i = 0; i < ( int )A.Length; i++)
if (Array.BinarySearch(B.ToCharArray(),
A[i]) > -1)
return true ;
return false ;
}
static int countPalindromicSubStrings(String []s,
int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1; j < n; j++)
if (isPossible(s[i], s[j]))
count++;
}
return count;
}
static String sortString(String inputString)
{
char []tempArray = inputString.ToCharArray();
Array.Sort(tempArray);
return new String(tempArray);
}
public static void Main(String[] args)
{
String []arr = { "gfg" , "gfg" };
int n = 2;
Console.Write(countPalindromicSubStrings(arr, n));
}
}
|
Javascript
<script>
function isPossible(A, B)
{
B = B.split( '' ).sort().join( '' );
var c=0;
for ( var i = 0; i < A.length; i++)
if (B.indexOf(A[i]) != -1)
return true ;
return false ;
}
function countPalindromicSubstrings(s, n)
{
var count = 0;
for ( var i = 0; i < n; i++){
for ( var j = i + 1; j < n; j++)
if (isPossible(s[i], s[j]))
count++;
}
return count;
}
var arr = [ "gfg" , "gfg" ]
var n = 2
document.write(countPalindromicSubstrings(arr, n))
</script>
|
Time complexity: O(n2*mlogm) where m is length of string
Auxiliary Space: O(1)
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...