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:
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:
 

 

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to count of 
// palindromic Palindromic Substrings
// that can be formed from the array
  
#include<bits/stdc++.h> 
using namespace std; 
  
// Function to to check if possible
// to make palindromic substring
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;
  
// Function to count of Palindromic Substrings
// that can be formed from the array.
int countPalindromicSubstrings(string s[], int n)
{
    // variable to store count
    int count = 0;
  
    // Traverse through all the pairs
    // in the array
    for(int i = 0; i < n; i++){
        for(int j = i + 1; j < n; j++)
            if(isPossible(s[i], s[j]))
                count++;
    }
    return count;
}
  
// Driver Code 
int main() 
    string arr[] = { "gfg", "gfg" }; 
    int n = 2;
    cout << countPalindromicSubstrings(arr, n);
    return 0; 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to count of 
// palindromic Palindromic SubStrings
// that can be formed from the array
import java.util.*;
  
class GFG{ 
  
// Function to to check if possible
// to make palindromic subString
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;
  
// Function to count of Palindromic SubStrings
// that can be formed from the array.
static int countPalindromicSubStrings(String s[],
                                      int n)
{
      
    // Variable to store count
    int count = 0;
  
    // Traverse through all the pairs
    // in the array
    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) 
      
    // Convert input string to char array 
    char tempArray[] = inputString.toCharArray(); 
          
    // Sort tempArray 
    Arrays.sort(tempArray); 
          
    // Return new sorted string 
    return new String(tempArray); 
  
// Driver Code 
public static void main(String[] args) 
    String arr[] = { "gfg", "gfg" }; 
    int n = 2;
      
    System.out.print(countPalindromicSubStrings(arr, n));
  
// This code is contributed by Rajput-Ji

chevron_right


Output: 

1

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Rajput-Ji