给定两个长度为N的二进制字符串A和B ,任务是检查是否可以通过反转包含偶数1 s的A子字符串来将字符串A转换为B。
例子:
Input: A = “10011”, B = “11100”
Output: Yes
Explanation: Reverse substring A[2, 5], 10011 → 11100.
After completing the above steps, strings A and B are the same.
Input: A = “10011” B = “00111”
Output: No
方法:该想法基于以下观察:
- 如果可以将字符串A转换为字符串B ,那么反之亦成立,因为将A转换为B的操作可以颠倒以将B转换为A。
- 只有在以下情况下才能使A等于B :
- 长度(A)=长度(B)以及A和B中的1的数目相同,并且
- Cnt A = Cnt B 其中cnt S是位置i的数量,其中1≤i≤length (S)且(∑ i j = 1 (S j ))mod 2 = 1 。
请按照以下步骤解决问题:
- 遍历字符串A和B ,并将频率1分别存储在变量count1A和count1B中。
- 初始化一个变量,例如temp ,以存储1s的临时计数。
- 使用变量i遍历字符串A并执行以下步骤:
- 如果当前字符为1 ,则将temp递增1 。
- 否则,如果temp的值为奇数,则将变量奇数1A增加1 。否则,将变量even1A递增1。
- 对字符串B也重复上述步骤2到3 。
- 完成上述步骤后,如果count1A和count1B的价值是相同的,odd1A和odd1B的价值是相同的,并且even1A和even1B的价值,是一样的,然后打印“是”其他打印“否”。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if string A can be
// transformed to string B by reversing
// substrings of A having even number of 1s
void canTransformStrings(string A, string B)
{
// Store the size of string A
int n1 = A.size();
// Store the size of string B
int n2 = B.size();
// Store the count of 1s in A and B
int count1A = 0, count1B = 0;
// Stores cntA for string A
// and cntB for string B
int odd1A = 0, odd1B = 0;
int even1A = 0, even1B = 0;
// Traverse the string A
for (int i = 0; i < n1; i++) {
// If current character is 1
if (A[i] == '1')
// Increment 1s count
count1A++;
// Otherwise, update odd1A or
// even1A depending whether
// count1A is odd or even
else {
if (count1A & 1)
odd1A++;
else
even1A++;
}
}
// Traverse the string B
for (int i = 0; i < n2; i++) {
// If current character is 1
if (B[i] == '1')
// Increment 1s count
count1B++;
// Otherwise, update odd1B or
// even1B depending whether
// count1B is odd or even
else {
if (count1B & 1)
odd1B++;
else
even1B++;
}
}
// If the condition is satisfied
if (count1A == count1B
&& odd1A == odd1B
&& even1A == even1B) {
// If true, print Yes
cout << "Yes";
}
// Otherwise, print No
else
cout << "No";
}
// Driver Code
int main()
{
string A = "10011", B = "11100";
// Function Call
canTransformStrings(A, B);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to check if string A can be
// transformed to string B by reversing
// substrings of A having even number of 1s
public static void canTransformStrings(String A,
String B)
{
// Store the size of string A
int n1 = A.length();
// Store the size of string B
int n2 = B.length();
// Store the count of 1s in A and B
int count1A = 0, count1B = 0;
// Stores cntA for string A
// and cntB for string B
int odd1A = 0, odd1B = 0;
int even1A = 0, even1B = 0;
// Traverse the string A
for(int i = 0; i < n1; i++)
{
// If current character is 1
if (A.charAt(i) == '1')
// Increment 1s count
count1A++;
// Otherwise, update odd1A or
// even1A depending whether
// count1A is odd or even
else
{
if ((count1A & 1) == 1)
odd1A++;
else
even1A++;
}
}
// Traverse the string B
for(int i = 0; i < n2; i++)
{
// If current character is 1
if (B.charAt(i) == '1')
// Increment 1s count
count1B++;
// Otherwise, update odd1B or
// even1B depending whether
// count1B is odd or even
else
{
if ((count1B & 1) == 1)
odd1B++;
else
even1B++;
}
}
// If the condition is satisfied
if (count1A == count1B &&
odd1A == odd1B &&
even1A == even1B)
{
// If true, print Yes
System.out.print("Yes");
}
// Otherwise, print No
else
System.out.print("No");
}
// Driver Code
public static void main(String[] args)
{
String A = "10011", B = "11100";
// Function Call
canTransformStrings(A, B);
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python program for the above approach
# Function to check if string A can be
# transformed to string B by reversing
# substrings of A having even number of 1s
def canTransformStrings(A, B):
# Store the size of string A
n1 = len(A);
# Store the size of string B
n2 = len(B);
# Store the count of 1s in A and B
count1A = 0;
count1B = 0;
# Stores cntA for string A
# and cntB for string B
odd1A = 0; odd1B = 0;
even1A = 0; even1B = 0;
# Traverse the string A
for i in range(n1):
# If current character is 1
if (A[i] == '1'):
# Increment 1s count
count1A += 1;
# Otherwise, update odd1A or
# even1A depending whether
# count1A is odd or even
else:
if ((count1A & 1) == 1):
odd1A += 1;
else:
even1A += 1;
# Traverse the string B
for i in range(n2):
# If current character is 1
if (B[i] == '1'):
# Increment 1s count
count1B += 1;
# Otherwise, update odd1B or
# even1B depending whether
# count1B is odd or even
else:
if ((count1B & 1) == 1):
odd1B += 1;
else:
even1B += 1;
# If the condition is satisfied
if (count1A == count1B and odd1A == odd1B and even1A == even1B):
# If True, prYes
print("Yes");
# Otherwise, prNo
else:
print("No");
# Driver Code
if __name__ == '__main__':
A = "10011";
B = "11100";
# Function Call
canTransformStrings(A, B);
# This code is contributed by Princi Singh
C#
// C# program for the above approach
using System;
using System.Collections;
class GFG {
// Function to check if string A can be
// transformed to string B by reversing
// substrings of A having even number of 1s
static void canTransformStrings(string A, string B)
{
// Store the size of string A
int n1 = A.Length;
// Store the size of string B
int n2 = B.Length;
// Store the count of 1s in A and B
int count1A = 0, count1B = 0;
// Stores cntA for string A
// and cntB for string B
int odd1A = 0, odd1B = 0;
int even1A = 0, even1B = 0;
// Traverse the string A
for(int i = 0; i < n1; i++)
{
// If current character is 1
if (A[i] == '1')
// Increment 1s count
count1A++;
// Otherwise, update odd1A or
// even1A depending whether
// count1A is odd or even
else
{
if ((count1A & 1) == 1)
odd1A++;
else
even1A++;
}
}
// Traverse the string B
for(int i = 0; i < n2; i++)
{
// If current character is 1
if (B[i] == '1')
// Increment 1s count
count1B++;
// Otherwise, update odd1B or
// even1B depending whether
// count1B is odd or even
else
{
if ((count1B & 1) == 1)
odd1B++;
else
even1B++;
}
}
// If the condition is satisfied
if (count1A == count1B &&
odd1A == odd1B &&
even1A == even1B)
{
// If true, print Yes
Console.Write("Yes");
}
// Otherwise, print No
else
Console.Write("No");
}
static void Main()
{
string A = "10011", B = "11100";
// Function Call
canTransformStrings(A, B);
}
}
// This code is contributed by divyesh072019
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)