Count pair of strings whose concatenation of substrings form a palindrome Last Updated : 09 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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++ // 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; } Java // 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 Python3 # Python3 implementation to count of # palindromic Palindromic Substrings # that can be formed from the array # Function to to check if possible # to make palindromic substring def isPossible(A, B): B = sorted(B) c = 0 for i in range(len(A)): if A[i] in B: return True return False # Function to count of Palindromic # Substrings that can be formed # from the array. def countPalindromicSubstrings(s, n): # Variable to store count count = 0 # Traverse through all # Substrings in the array for i in range(n): for j in range(i + 1, n): if(isPossible(s[i], s[j])): count += 1 return count # Driver Code arr = ["gfg", "gfg"] n = 2 print(countPalindromicSubstrings(arr, n)) # This code is contributed by avanitrachhadiya2155 C# // C# implementation to count of // palindromic Palindromic SubStrings // that can be formed from the array using System; class GFG{ // Function to to check if possible // to make palindromic subString 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; } // 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 Array.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; Console.Write(countPalindromicSubStrings(arr, n)); } } // This code is contributed by Rajput-Ji JavaScript <script> // Javascript implementation // Function to to check if possible // to make palindromic substring 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 to count of Palindromic Substrings // that can be formed from the array. function countPalindromicSubstrings(s, n) { // variable to store count var count = 0; // Traverse through all the pairs // in the array for(var i = 0; i < n; i++){ for(var j = i + 1; j < n; j++) if(isPossible(s[i], s[j])) count++; } return count; } // Driver Code var arr = ["gfg", "gfg"] var n = 2 document.write(countPalindromicSubstrings(arr, n)) // This code is contributed by shubhamsingh10 </script> Output: 1 Time complexity: O(n2*mlogm) where m is length of string Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Count of pairs of strings whose concatenation forms a palindromic string S spp____ Follow Improve Article Tags : Strings Combinatorial DSA Arrays palindrome substring +2 More Practice Tags : ArraysCombinatorialpalindromeStrings Similar Reads Count of pairs of strings whose concatenation forms a palindromic string Given an array A[ ] consisting of N strings, the task is to count the number of pairs of possible strings that on merging forms a Palindromic String or can be rearranged to form a Palindromic String. Example : Input: N = 6, A[ ] = {aab, abcac, dffe, ed, aa, aade}Output: 6Explanation: All possible pa 9 min read 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.E 7 min read Check if concatenation of splitted substrings of two given strings forms a palindrome or not Given two strings a and b of the same length, the task is to check if splitting both the strings and concatenating their opposite substrings, i.e. concatenating the left substring of a with right substring of b or concatenating the left substring of b with right substring of a, forms a palindrome or 8 min read Count substrings of a given string whose anagram is a palindrome Given a string S of length N containing only lowercase alphabets, the task is to print the count of substrings of the given string whose anagram is palindromic. Examples: Input: S = "aaaa"Output: 10Explanation:Possible substrings are {"a", "a", "a", "a", "aa", "aa", "aa", "aaa", "aaa", "aaaa"}. Sinc 10 min read Count pairs of non overlapping Substrings of size K which form a palindromic String Given a string S of length l, the task is to count the number of pairs of two non-overlapping substrings of length K of the string S, such that on joining the two non-overlapping substrings the resultant string is a palindrome. Examples: Input: S = "abcdbadc", K = 2Output: 2 Explanation: Possible su 15 min read Count of Palindrome Strings in given Array of strings Given an array of strings arr[] of size N where each string consists only of lowercase English letter. The task is to return the count of all palindromic string in the array. Examples: Input: arr[] = {"abc","car","ada","racecar","cool"}Output: 2Explanation: "ada" and "racecar" are the two palindrome 5 min read Like