📌  相关文章
📜  通过交换成对的不等字符来检查两个二进制字符串可以相等

📅  最后修改于: 2021-04-26 05:40:39             🧑  作者: Mango

给定两个长度为N ( 1≤N≤10 5 )的二进制字符串S1S2 ,任务是检查是否有可能将字符串S1转换为
S2通过多次执行以下操作:

  1. 选择任意两个索引ij ( 1≤i ),以使S1 [i]‘0’S1 [j]‘1’
  2. S1 [i]S1 [j]交换。

例子:

方法:请按照以下步骤解决问题:

  1. 检查两个字符串中字符“ 0”“ 1”的出现次数是否相等。如果发现不是真的,则不可能将字符串S1转换为S2
  2. 如果字符数相等,则转到下一步。在给定条件下,通过与字符串S1中的字母“ 1”交换,可以仅将“ 0”向前移动
  3. 因此,两个字符串的迭代字符和计数在两个字符串的“0”的出现次数。如果在任何时候字符串S2中的字符“ 0”的数量严格大于字符串S1中出现的字符串的数量,则终止循环并打印“ NO”
  4. 如果两个字符串成功迭代,则打印“ 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


输出:
YES
NO

时间复杂度: O(N)
辅助空间:O(1)