给定两个长度为N ( 1 ≤ N ≤ 10 5 ) 的二进制字符串S1和S2 ,任务是检查是否可以将字符串S1转换为
S2通过执行以下操作任意次数:
- 选择任意两个索引i和j ( 1 ≤ i < j ≤ N ),使得S1[i]为‘0’且S1[j]为‘1’ 。
- 将S1[i]与S1[j]交换。
例子:
Input: S1 =”100111″, S2 = “111010”
Output: YES
Explanation:Swap S[2] and S[3] with S[4] and S[6] respectively.
Input: S1 = “110100”, S2 = “010101”
Output: NO
处理方法:按照以下步骤解决问题:
- 检查两个字符串中字符“0”和“1”的出现次数是否相等。如果发现不为真,则不可能将字符串S1转换为S2 。
- 如果字符数相等,则进入下一步。根据给定的条件,通过与字符串S1 中的字母 ‘1’ 交换,可以仅向前移动 ‘0’。
- 因此,两个字符串的迭代字符和计数在两个字符串的“0”的出现次数。如果在任何时候在字符串S的“0”的字符个数比出现在字符串S1的计数变成严格大于终止循环并打印“NO”。
- 如果两个字符串迭代成功,则打印“YES” 。
下面是上述方法的实现:
C++
// C++ Program to implement
// of above approach
#include
using namespace std;
// Function to check if a string
// s1 can be converted into s2
void check(string s1, string s2)
{
// Count of '0' in strings in s1 and s2
int s1_0 = 0, s2_0 = 0;
// Iterate both the strings and
// count the number of occurrences of
for (int i = 0; i < s1.size(); i++) {
if (s1[i] == '0') {
s1_0++;
}
if (s2[i] == '0') {
s2_0++;
}
}
// Count is not equal
if (s1_0 != s2_0) {
cout << "NO" << endl;
return;
}
else {
int Count1 = 0, Count2 = 0;
// Iterating over both the
// arrays and count the
// number of occurrences of '0'
for (int i = 0; i < s1.size(); i++) {
if (s1[i] == '0') {
Count1++;
}
if (s2[i] == '0') {
Count2++;
}
// If the count of occurrences
// of '0' in S2 exceeds that in S1
if (Count1 < Count2) {
cout << "NO" << endl;
return;
}
}
cout << "YES" << endl;
}
}
// Driver program
int main()
{
string s1 = "100111";
string s2 = "111010";
check(s1, s2);
s1 = "110100";
s2 = "010101";
check(s1, s2);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Function to check if a string
// s1 can be converted into s2
static void check(String s1, String s2)
{
// Count of '0' in strings in s1 and s2
int s1_0 = 0, s2_0 = 0;
// Iterate both the strings and
// count the number of occurrences of
for(int i = 0; i < s1.length(); i++)
{
if (s1.charAt(i) == '0')
{
s1_0++;
}
if (s2.charAt(i) == '0')
{
s2_0++;
}
}
// Count is not equal
if (s1_0 != s2_0)
{
System.out.println("NO");
return;
}
else
{
int Count1 = 0, Count2 = 0;
// Iterating over both the
// arrays and count the
// number of occurrences of '0'
for(int i = 0; i < s1.length(); i++)
{
if (s1.charAt(i) == '0')
{
Count1++;
}
if (s2.charAt(i) == '0')
{
Count2++;
}
// If the count of occurrences
// of '0' in S2 exceeds that in S1
if (Count1 < Count2)
{
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
}
// Driver Code
public static void main(String[] args)
{
String s1 = "100111";
String s2 = "111010";
check(s1, s2);
s1 = "110100";
s2 = "010101";
check(s1, s2);
}
}
// This code is contributed by code_hunt.
Python3
# Python3 program to implement
# of above approach
# Function to check if a string
# s1 can be converted into s2
def check(s1, s2):
# Count of '0' in strings in s1 and s2
s1_0 = 0
s2_0 = 0
# Iterate both the strings and
# count the number of occurrences of
for i in range(len(s1)):
if (s1[i] == '0'):
s1_0 += 1
if (s2[i] == '0'):
s2_0 += 1
# Count is not equal
if (s1_0 != s2_0):
print("NO")
return
else:
Count1 = 0
Count2 = 0;
# Iterating over both the
# arrays and count the
# number of occurrences of '0'
for i in range(len(s1)):
if (s1[i] == '0'):
Count1 += 1
if (s2[i] == '0'):
Count2 += 1
# If the count of occurrences
# of '0' in S2 exceeds that in S1
if (Count1 < Count2):
print("NO")
return
print("YES")
# Driver code
if __name__ == "__main__":
s1 = "100111"
s2 = "111010"
check(s1, s2)
s1 = "110100"
s2 = "010101"
check(s1, s2)
# This code is contributed by chitranayal
C#
// C# program to implement
// of above approach
using System;
class GFG{
// Function to check if a string
// s1 can be converted into s2
static void check(string s1, string s2)
{
// Count of '0' in strings in s1 and s2
int s1_0 = 0, s2_0 = 0;
// Iterate both the strings and
// count the number of occurrences of
for(int i = 0; i < s1.Length; i++)
{
if (s1[i] == '0')
{
s1_0++;
}
if (s2[i] == '0')
{
s2_0++;
}
}
// Count is not equal
if (s1_0 != s2_0)
{
Console.WriteLine("NO");
return;
}
else
{
int Count1 = 0, Count2 = 0;
// Iterating over both the
// arrays and count the
// number of occurrences of '0'
for(int i = 0; i < s1.Length; i++)
{
if (s1[i] == '0')
{
Count1++;
}
if (s2[i] == '0')
{
Count2++;
}
// If the count of occurrences
// of '0' in S2 exceeds that in S1
if (Count1 < Count2)
{
Console.WriteLine("NO");
return;
}
}
Console.WriteLine("YES");
}
}
// Driver code
static void Main()
{
string s1 = "100111";
string s2 = "111010";
check(s1, s2);
s1 = "110100";
s2 = "010101";
check(s1, s2);
}
}
// This code is contributed by divyesh072019
Javascript
输出:
YES
NO
时间复杂度: O(N)
辅助空间:O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live