📌  相关文章
📜  检查两个给定字符串的拆分子字符串的串联是否形成回文

📅  最后修改于: 2021-05-06 19:54:04             🧑  作者: Mango

给定两个字符串长度相同的B,任务是检查是否分裂两个字符串和连接它们的对面子,即串联左侧子与B的右子或右子串联B的左子a或不形成回文。如果发现是真的,则打印“是” 。否则,打印“否”

注意:拆分的子字符串之一可以为空。

例子:

方法:想法是使用两指针技术来解决此问题。请按照以下步骤解决问题:

  1. 将指针i放置在a0索引处,“ j”放置在b的最后一个索引处。
  2. 迭代字符串和检查的字符,如果A [1] == B [j]时,则递增i和递减Ĵ。
  3. 否则,只需中断循环即可,因为它不是回文式类型的序列。
  4. aLeftbRight连接到字符串变量xa中,并将aRightbLeft连接到另一个字符串变量xb中
  5. 检查tw字符串中的任意一个是否是回文。如果发现是真的,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if concatenating
// opposite substrings after splitting
// two given strings forms a palindrome
// or not
bool checkSplitting(string a, string b)
{
     
    // Length of the string
    int len = a.length();
    int i = 0, j = len - 1;
     
    // Iterate through the strings
    while (i < len)
    {
         
        // If not a palindrome sequence
        if (a[i] != b[j])
        {
            break;
        }
        i += 1;
        j -= 1;
 
        // Concatenate left substring of a
        // and right substring of b in xa
        // Concatenate right substring of a
        // and left substring of b in xb
        string xa = a.substr(i, j + 1);
        string xb = b.substr(i, j + 1);
 
        // Check if either of the two concatenated
        // strings is a palindrom or not
        if (xa == string(xa.rbegin(), xa.rend()) ||
            xb == string(xb.rbegin(), xb.rend()))
            return true;
    }
}
 
// Function to check if concatenation of splitted
// substrings of two given strings forms a palindrome
void isSplitPossible(string a, string b)
{
    if (checkSplitting(a, b) == true)
    {
        cout << "Yes";
    }
    else if (checkSplitting(b, a) == true)
    {
        cout << "Yes";
    }
    else
    {
        cout << "No";
    }
}
 
// Driver Code
int main()
{
    string a = "ulacfd", b = "jizalu";
 
    // Function Call
    isSplitPossible(a, b);
     
    return 0;
}
 
// This code is contributed by pushpendrayadav1057


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if concatenating
// opposite subStrings after splitting
// two given Strings forms a palindrome
// or not
static boolean checkSplitting(String a, String b)
{
     
    // Length of the String
    int len = a.length();
    int i = 0, j = len - 1;
     
    // Iterate through the Strings
    while (i < len)
    {
         
        // If not a palindrome sequence
        if (a.charAt(i) != b.charAt(j))
        {
            break;
        }
        i += 1;
        j -= 1;
 
        // Concatenate left subString of a
        // and right subString of b in xa
        // Concatenate right subString of a
        // and left subString of b in xb
        String xa = a.substring(i, j + 1);
        String xb = b.substring(i, j + 1);
 
        // Check if either of the two concatenated
        // Strings is a palindrom or not
        if (xa.equals(reverse(xa))||xb.equals(reverse(xb)))
            return true;
    }
    return false;
}
 
// Function to check if concatenation of splitted
// subStrings of two given Strings forms a palindrome
static void isSplitPossible(String a, String b)
{
    if (checkSplitting(a, b) == true)
    {
        System.out.print("Yes");
    }
    else if (checkSplitting(b, a) == true)
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
static String reverse(String input) {
    char[] a = input.toCharArray();
    int l, r = a.length - 1;
    for (l = 0; l < r; l++, r--) {
        char temp = a[l];
        a[l] = a[r];
        a[r] = temp;
    }
    return String.valueOf(a);
}
   
// Driver Code
public static void main(String[] args)
{
    String a = "ulacfd", b = "jizalu";
 
    // Function Call
    isSplitPossible(a, b);   
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to check if concatenating
# opposite substrings after splitting
# two given strings forms a palindrome or not
def checkSplitting(a, b, n):
    i, j = 0, n - 1
 
    # Iterate through the strings
    while(i < n):
 
         
        # If not a palindrome sequence
        if(a[i] != b[j]):
            break
        i += 1
        j -= 1
 
        # Concatenate left substring of a
        # and right substring of b in xa
        # Concatenate right substring of a
        # and left substring of b in xb
        xa = a[i:j + 1]
        xb = b[i:j + 1]
 
        # Check if either of the two concatenated
        # strings is a palindrom or not
        if(xa == xa[::-1] or xb == xb[::-1]):
            return True
 
# Function to check if concatenation of splitted
# substrings of two given strings forms a palindrome
def isSplitPossible(a, b):
    if checkSplitting(a, b, len(a)) == True:
        print("Yes")
         
    elif checkSplitting(b, a, len(a)) == True:
        print("Yes")
         
    else:
        print("No")
 
 
# Given string a and b
a = "ulacfd"
b = "jizalu"
 
# Function Call
isSplitPossible(a, b)


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if concatenating
// opposite subStrings after splitting
// two given Strings forms a palindrome
// or not
static bool checkSplitting(String a, String b)
{
     
    // Length of the String
    int len = a.Length;
    int i = 0, j = len - 1;
     
    // Iterate through the Strings
    while (i < len)
    {
         
        // If not a palindrome sequence
        if (a[i] != b[j])
        {
            break;
        }
        i += 1;
        j -= 1;
 
        // Concatenate left subString of a
        // and right subString of b in xa
        // Concatenate right subString of a
        // and left subString of b in xb
        String xa = a.Substring(i, j + 1 - i);
        String xb = b.Substring(i, j + 1 - i);
 
        // Check if either of the two concatenated
        // Strings is a palindrom or not
        if (xa.Equals(reverse(xa)) ||
            xb.Equals(reverse(xb)))
            return true;
    }
    return false;
}
 
// Function to check if concatenation of splitted
// subStrings of two given Strings forms a palindrome
static void isSplitPossible(String a, String b)
{
    if (checkSplitting(a, b) == true)
    {
        Console.Write("Yes");
    }
    else if (checkSplitting(b, a) == true)
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
static String reverse(String input)
{
    char[] a = input.ToCharArray();
    int l, r = a.Length - 1;
    for (l = 0; l < r; l++, r--)
    {
        char temp = a[l];
        a[l] = a[r];
        a[r] = temp;
    }
    return String.Join("",a);
}
   
// Driver Code
public static void Main(String[] args)
{
    String a = "ulacfd", b = "jizalu";
 
    // Function Call
    isSplitPossible(a, b);   
}
}
 
// This code is contributed by 29AjayKumar


输出
Yes

时间复杂度: O(N)
辅助空间: O(1)