给定两个字符串str1和str2 ,任务是检查是否可以通过串联str1和str2的两个子字符串来形成回文字符串。
例子:
Input: str1 = “abcd”, str2 = “acba”
Output: Yes
Explanation:
There are five possible cases where concatenation of two substrings from str1 and str2 gives palindromic string:
“ab” + “a” = “aba”
“ab” + “ba” = “abba”
“bc” + “cb” = “bccb”
“bc” + “b” = “bcb”
“cd” + “c” = “cdc”
Input: str1 = “pqrs”, str2 = “abcd”
Output: No
Explanation:
There is no possible concatenation of sub-strings from given strings which gives palindromic string.
天真的方法:
解决问题的最简单方法是生成str1和str2 的每个可能的子字符串,并将它们组合起来生成所有可能的连接。对于每个串联,检查它是否是回文的。如果发现是真的,打印“是” 。否则,打印“否” 。
时间复杂度: O(N 2 * M 2 * (N+M)),其中 N 和 M 分别是 str1 和 str2 的长度。
辅助空间: O(1)
有效的方法:
为了优化上述方法,需要进行以下观察:
If the given strings possess at least one common character, then they will always form a palindromic string on concatenation of the common character from both the strings.
Illustration:
str1 = “abc”, str2 = “fad”
Since ‘a’ is common in both strings, a palindromic string “aa” can be obtained.
请按照以下步骤解决问题:
- 初始化一个布尔数组以标记两个字符串中每个字母的存在。
- 遍历str1并将索引(str1[i] – ‘a’)标记为真。
- 现在,遍历str2并检查是否有任何索引(str2[i] – ‘a’)已标记为true ,打印“Yes” 。
- 完全遍历str2 后,如果没有找到公共字符,则打印“No”。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to check if a palindromic
// string can be formed from the
// substring of given strings
bool check(string str1, string str2)
{
// Boolean array to mark
// presence of characters
vector mark(26, false);
int n = str1.size(),
m = str2.size();
for (int i = 0; i < n; i++) {
mark[str1[i] - 'a'] = true;
}
// Check if any of the character
// of str2 is already marked
for (int i = 0; i < m; i++) {
// If a common character
// is found
if (mark[str2[i] - 'a'])
return true;
}
// If no common character
// is found
return false;
}
// Driver Code
int main()
{
string str1 = "abca",
str2 = "efad";
if (check(str1, str2))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.Arrays;
class GFG{
// Function to check if a palindromic
// string can be formed from the
// substring of given strings
public static boolean check(String str1,
String str2)
{
// Boolean array to mark
// presence of characters
boolean[] mark = new boolean[26];
Arrays.fill(mark, false);
int n = str1.length(),
m = str2.length();
for(int i = 0; i < n; i++)
{
mark[str1.charAt(i) - 'a'] = true;
}
// Check if any of the character
// of str2 is already marked
for(int i = 0; i < m; i++)
{
// If a common character
// is found
if (mark[str2.charAt(i) - 'a'])
return true;
}
// If no common character
// is found
return false;
}
// Driver code
public static void main(String[] args)
{
String str1 = "abca",
str2 = "efad";
if (check(str1, str2))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program to implement
# the above approach
# Function to check if a palindromic
# string can be formed from the
# substring of given strings
def check(str1, str2):
# Boolean array to mark
# presence of characters
mark = [False for i in range(26)]
n = len(str1)
m = len(str2)
for i in range(n):
mark[ord(str1[i]) - ord('a')] = True
# Check if any of the character
# of str2 is already marked
for i in range(m):
# If a common character
# is found
if (mark[ord(str2[i]) - ord('a')]):
return True;
# If no common character
# is found
return False
# Driver code
if __name__=="__main__":
str1 = "abca"
str2 = "efad"
if (check(str1, str2)):
print("Yes");
else:
print("No");
# This code is contributed by rutvik_56
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to check if a palindromic
// string can be formed from the
// substring of given strings
public static bool check(String str1,
String str2)
{
// Boolean array to mark
// presence of characters
bool[] mark = new bool[26];
int n = str1.Length,
m = str2.Length;
for(int i = 0; i < n; i++)
{
mark[str1[i] - 'a'] = true;
}
// Check if any of the character
// of str2 is already marked
for(int i = 0; i < m; i++)
{
// If a common character
// is found
if (mark[str2[i] - 'a'])
return true;
}
// If no common character
// is found
return false;
}
// Driver code
public static void Main(String[] args)
{
String str1 = "abca",
str2 = "efad";
if (check(str1, str2))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by amal kumar choubey
Javascript
Yes
时间复杂度: O(max(N, M)) 其中 N 和 M 分别是 str1 和 str2 的长度。
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live