📜  计算其子串连接形成回文的成对的字符串

📅  最后修改于: 2021-04-22 10:09:50             🧑  作者: Mango

给定一个字符串数组arr [] ,任务是计算其子字符串串联形成回文的一对字符串。
例子:

方法:问题中的主要观察结果是,如果两个字符串都至少具有一个共同的字符(假设为’c’),那么我们就可以形成回文字符串。因此,请检查数组中所有对是否在字符串中存在一个公共字符。
下面是上述方法的实现:

C++
// C++ implementation to count of 
// palindromic Palindromic Substrings
// that can be formed from the array
  
#include 
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


输出:
1