给定两个长度为N 的二进制字符串A和B ,任务是检查字符串A 是否可以通过反转包含偶数个1的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 :
- Length(A) = Length(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 的值为奇数,则将变量odd1A增加1 。否则,将变量even1A增加 1。
- 对字符串B也重复上述步骤2到3 。
- 完成上述步骤后,如果count1A和count1B的值相同, odd1A和odd1B的值相同,并且even1A和even1B的值相同,则打印“Yes”,否则打印“No” 。
下面是上述方法的实现:
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
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live