给定两个大小为N的字符串str1和str2 ,该字符串仅由三个字符A , B和C组成,任务是使用以下操作检查是否可以将字符串str1更改为str2 :
- 用“ CB”替换出现的“ BC”,即交换相邻的“ B”和“ C”。
- 用“ AC”替换一次出现的“ CA”,即交换相邻的“ C”和“ A”。
如果我们可以转换字符串,则打印“是” ,否则打印“否” 。
例子:
Input: str1 = “BCCABCBCA”, str2 = “CBACCBBAC”
Output: Yes
Explanation:
Transform the strings using following these steps:
BCCABCBCA -> CBCABCBCA -> CBACBCBCA -> CBACCBBCA -> CBACCBBAC.
Input: str1 = “BAC”, str2 = “CAB”
Output: False
天真的方法:这个想法是通过执行给定的操作从字符串str1的开头递归地生成所有可能的字符串,并将其存储在一组字符串。然后检查集合中的任何字符串是否等于字符串str2 。如果在集合中找到字符串str2 ,则打印“是”,否则打印“否” 。
时间复杂度: O(2 N )
辅助空间: O(1)
高效的方法:想法是同时横切两个字符串,并检查是否有可能将字符串str1转换为str2直到特定的索引。步骤如下:
- 检查字符串str1和str2中的序列’A’和’B’,如果相同,则继续执行第二步。否则,打印“否”,因为无法进行所需的转换。
- str1中的“ A”索引应大于和等于str2中相应的“ A”的索引,因为“ CA”只能转换为“ AC”。
- 同样, str1字符串的“ B”索引应小于或等于str2中相应的“ B”索引,因为“ BC”只能转换为“ CB”。
- 如果不满足以上两个条件,则打印“否” 。否则,打印“是” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if it is possible
// to transform start to end
bool canTransform(string str1,
string str2)
{
string s1 = "";
string s2 = "";
// Check the sequence of A, B in
// both strings str1 and str2
for (char c : str1) {
if (c != 'C') {
s1 += c;
}
}
for (char c : str2) {
if (c != 'C') {
s2 += c;
}
}
// If both the strings
// are not equal
if (s1 != s2)
return false;
int i = 0;
int j = 0;
int n = str1.length();
// Traverse the strings
while (i < n and j < n) {
if (str1[i] == 'C') {
i++;
}
else if (str2[j] == 'C') {
j++;
}
// Check for indexes of A and B
else {
if ((str1[i] == 'A'
and i < j)
or (str1[i] == 'B'
and i > j)) {
return false;
}
i++;
j++;
}
}
return true;
}
// Driver Code
int main()
{
string str1 = "BCCABCBCA";
string str2 = "CBACCBBAC";
// Function Call
if (canTransform(str1, str2)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if it is possible
// to transform start to end
static boolean canTransform(String str1, String str2)
{
String s1 = "";
String s2 = "";
// Check the sequence of A, B in
// both Strings str1 and str2
for (char c : str1.toCharArray())
{
if (c != 'C')
{
s1 += c;
}
}
for (char c : str2.toCharArray())
{
if (c != 'C')
{
s2 += c;
}
}
// If both the Strings
// are not equal
if (!s1.equals(s2))
return false;
int i = 0;
int j = 0;
int n = str1.length();
// Traverse the Strings
while (i < n && j < n)
{
if (str1.charAt(i) == 'C')
{
i++;
}
else if (str2.charAt(j) == 'C')
{
j++;
}
// Check for indexes of A and B
else
{
if ((str1.charAt(i) == 'A' && i < j) ||
(str1.charAt(i) == 'B' && i > j))
{
return false;
}
i++;
j++;
}
}
return true;
}
// Driver Code
public static void main(String[] args)
{
String str1 = "BCCABCBCA";
String str2 = "CBACCBBAC";
// Function Call
if (canTransform(str1, str2))
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function to check if it is possible
# to transform start to end
def canTransform(str1, str2):
s1 = ""
s2 = ""
# Check the sequence of A, B in
# both strings str1 and str2
for c in str1:
if (c != 'C'):
s1 += c
for c in str2:
if (c != 'C'):
s2 += c
# If both the strings
# are not equal
if (s1 != s2):
return False
i = 0
j = 0
n = len(str1)
# Traverse the strings
while (i < n and j < n):
if (str1[i] == 'C'):
i += 1
elif (str2[j] == 'C'):
j += 1
# Check for indexes of A and B
else:
if ((str1[i] == 'A' and i < j) or
(str1[i] == 'B' and i > j)):
return False
i += 1
j += 1
return True
# Driver Code
if __name__ == '__main__':
str1 = "BCCABCBCA"
str2 = "CBACCBBAC"
# Function call
if (canTransform(str1, str2)):
print("Yes")
else:
print("No")
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if it is possible
// to transform start to end
static bool canTransform(string str1, string str2)
{
string s1 = "";
string s2 = "";
// Check the sequence of A, B in
// both Strings str1 and str2
foreach(char c in str1.ToCharArray())
{
if (c != 'C')
{
s1 += c;
}
}
foreach(char c in str2.ToCharArray())
{
if (c != 'C')
{
s2 += c;
}
}
// If both the Strings
// are not equal
if (s1 != s2)
return false;
int i = 0;
int j = 0;
int n = str1.Length;
// Traverse the Strings
while (i < n && j < n)
{
if (str1[i] == 'C')
{
i++;
}
else if (str2[j] == 'C')
{
j++;
}
// Check for indexes of A and B
else
{
if ((str1[i] == 'A' && i < j) ||
(str1[i] == 'B' && i > j))
{
return false;
}
i++;
j++;
}
}
return true;
}
// Driver Code
public static void Main(string[] args)
{
string str1 = "BCCABCBCA";
string str2 = "CBACCBBAC";
// Function call
if (canTransform(str1, str2))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code is contributed by rutvik_56
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)