📌  相关文章
📜  检查来自三个给定字符串的子字符串可以连接以形成回文

📅  最后修改于: 2021-09-07 04:12:07             🧑  作者: Mango

给定三个长度分别为LMN 的字符串S1S2S3 ,任务是检查是否可以从S1、S2S3 中选择一些非空子串使得它们的串联是回文。如果发现是真的,打印“YES” 。否则,打印“NO”。

例子:

方法:这个想法是要观察到,如果S1S3至少有一个公共字符,那么总是有可能找到这样的abc ,使得a+b+c变成回文。请按照以下步骤解决问题:

  1. 初始化两个变量,说MASKAmaskC,在字符串分别S1S3屏蔽字符。
  2. 遍历字符串S1和组(I-‘A’)MASKA比特,表示各个字符存在于字符串S1的字符。
  3. 遍历字符串S3和组(I-‘A’)maskC比特,表示各个字符存在于字符串S3的字符。
  4. 如果maskAmaskC的按位 AND 大于零,则打印“YES” 。否则,打印“NO”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if substrings from
// three given strings can be concatenated
// to form a palindrome
string make_palindrome(
    string S1, string S2, string S3)
{
    // Mask for S1 and S2
    int maskA = 0, maskC = 0;
 
    // Set (i-'a')th bit in maskA
    for (char i : S1)
        maskA |= (1 << (i - 'a'));
 
    // Set (i-'a')th bit in maskC
    for (char i : S3)
        maskC |= (1 << (i - 'a'));
 
    // If the bitwise AND is > 0
    if ((maskA & maskC) > 0)
        return "YES";
 
    return "NO";
}
 
// Driver Code
int main()
{
    string S1 = "adcb", S2 = "bcdb", S3 = "abe";
    cout << make_palindrome(S1, S2, S3);
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to check if subStrings from
// three given Strings can be concatenated
// to form a palindrome
static String make_palindrome(
    String S1, String S2, String S3)
{
   
    // Mask for S1 and S2
    int maskA = 0, maskC = 0;
 
    // Set (i-'a')th bit in maskA
    for (char i : S1.toCharArray())
        maskA |= (1 << (i - 'a'));
 
    // Set (i-'a')th bit in maskC
    for (char i : S3.toCharArray())
        maskC |= (1 << (i - 'a'));
 
    // If the bitwise AND is > 0
    if ((maskA & maskC) > 0)
        return "YES";
    return "NO";
}
 
// Driver Code
public static void main(String[] args)
{
    String S1 = "adcb", S2 = "bcdb", S3 = "abe";
    System.out.print(make_palindrome(S1, S2, S3));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to check if substrings from
# three given strings can be concatenated
# to form a palindrome
def make_palindrome(S1, S2, S3):
   
    # Mask for S1 and S2
    maskA, maskC = 0, 0
 
    # Set (i-'a')th bit in maskA
    for i in S1:
        maskA |= (1 << (ord(i) - ord('a')))
 
    # Set (i-'a')th bit in maskC
    for i in S3:
        maskC |= (1 << (ord(i) - ord('a')))
 
    # If the bitwise AND is > 0
    if ((maskA & maskC) > 0):
        return "YES"
 
    return "NO"
 
# Driver Code
if __name__ == '__main__':
    S1,S2,S3 = "adcb", "bcdb", "abe"
    print (make_palindrome(S1, S2, S3))
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
public class GFG
{
 
// Function to check if subStrings from
// three given Strings can be concatenated
// to form a palindrome
static String make_palindrome(
    String S1, String S2, String S3)
{
   
    // Mask for S1 and S2
    int maskA = 0, maskC = 0;
 
    // Set (i-'a')th bit in maskA
    foreach (char i in S1.ToCharArray())
        maskA |= (1 << (i - 'a'));
 
    // Set (i-'a')th bit in maskC
    foreach (char i in S3.ToCharArray())
        maskC |= (1 << (i - 'a'));
 
    // If the bitwise AND is > 0
    if ((maskA & maskC) > 0)
        return "YES";
    return "NO";
}
 
// Driver Code
public static void Main(String[] args)
{
    String S1 = "adcb", S2 = "bcdb", S3 = "abe";
    Console.Write(make_palindrome(S1, S2, S3));
}
}
 
// This code is contributed by Rajput-Ji


输出:
YES

时间复杂度: O(L + N)
辅助空间: O(L + M + N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live