三个非重叠子串的计数,它们在连接时形成回文
给定一个字符串str ,任务是计算通过串联字符串str的三个子字符串x 、 y和z可以形成回文子字符串的方式的数量,使得所有这些子字符串都不重叠,即子-字符串y出现在子字符串x之后,子字符串z出现在子字符串y之后。
例子:
Input: str = “abca”
Output: 2
The two valid pairs are (“a”, “b”, “a”) and (“a”, “c”, “a”)
Input: str = “abba”
Output: 5
方法:找到所有可能的三个非重叠子串对,并为每一对检查由它们连接生成的字符串是否为回文。如果是,则增加计数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that returns true if
// s[i...j] + s[k...l] + s[p...q]
// is a palindrome
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;
}
// Function to return the count
// of valid sub-strings
int countSubStr(string s)
{
// To store the count of
// required sub-strings
int count = 0;
int n = s.size();
// For choosing the first sub-string
for (int i = 0; i < n - 2; i++) {
for (int j = i; j < n - 2; j++) {
// For choosing the second sub-string
for (int k = j + 1; k < n - 1; k++) {
for (int l = k; l < n - 1; l++) {
// For choosing the third sub-string
for (int p = l + 1; p < n; p++) {
for (int q = p; q < n; q++) {
// Check if the concatenation
// is a palindrome
if (isPalin(i, j, k, l, p, q, s)) {
count++;
}
}
}
}
}
}
}
return count;
}
// Driver code
int main()
{
string s = "abca";
cout << countSubStr(s);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function that returns true if
// s[i...j] + s[k...l] + s[p...q]
// is a palindrome
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;
}
// Function to return the count
// of valid sub-strings
static int countSubStr(String s)
{
// To store the count of
// required sub-strings
int count = 0;
int n = s.length();
// For choosing the first sub-string
for (int i = 0; i < n - 2; i++)
{
for (int j = i; j < n - 2; j++)
{
// For choosing the second sub-string
for (int k = j + 1; k < n - 1; k++)
{
for (int l = k; l < n - 1; l++)
{
// For choosing the third sub-string
for (int p = l + 1; p < n; p++)
{
for (int q = p; q < n; q++)
{
// Check if the concatenation
// is a palindrome
if (isPalin(i, j, k, l, p, q, s))
{
count++;
}
}
}
}
}
}
}
return count;
}
// Driver code
public static void main(String[] args)
{
String s = "abca";
System.out.println(countSubStr(s));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
# Function that returns true if
# s[i...j] + s[k...l] + s[p...q]
# is a palindrome
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;
# Function to return the count
# of valid sub-strings
def countSubStr(s) :
# To store the count of
# required sub-strings
count = 0;
n = len(s);
# For choosing the first sub-string
for i in range(n-2) :
for j in range(i, n-2) :
# For choosing the second sub-string
for k in range(j + 1, n-1) :
for l in range(k, n-1) :
# For choosing the third sub-string
for p in range(l + 1, n) :
for q in range(p, n) :
# Check if the concatenation
# is a palindrome
if (isPalin(i, j, k, l, p, q, s)) :
count += 1;
return count;
# Driver code
if __name__ == "__main__" :
s = "abca";
print(countSubStr(s));
# This course is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function that returns true if
// s[i...j] + s[k...l] + s[p...q]
// is a palindrome
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;
}
// Function to return the count
// of valid sub-strings
static int countSubStr(String s)
{
// To store the count of
// required sub-strings
int count = 0;
int n = s.Length;
// For choosing the first sub-string
for (int i = 0; i < n - 2; i++)
{
for (int j = i; j < n - 2; j++)
{
// For choosing the second sub-string
for (int k = j + 1; k < n - 1; k++)
{
for (int l = k; l < n - 1; l++)
{
// For choosing the third sub-string
for (int p = l + 1; p < n; p++)
{
for (int q = p; q < n; q++)
{
// Check if the concatenation
// is a palindrome
if (isPalin(i, j, k, l, p, q, s))
{
count++;
}
}
}
}
}
}
}
return count;
}
// Driver code
public static void Main(String[] args)
{
String s = "abca";
Console.WriteLine(countSubStr(s));
}
}
// This code is contributed by Princi Singh
Javascript
输出:
2