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

📅  最后修改于: 2021-09-06 06:42:12             🧑  作者: Mango

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

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

例子:

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

  1. 检查两个字符串中字符“0”“1”的出现次数是否相等。如果发现不为真,则不可能将字符串S1转换为S2
  2. 如果字符数相等,则进入下一步。根据给定的条件,通过与字符串S1 中的字母 ‘1’ 交换,可以仅向前移动 ‘0’。
  3. 因此,两个字符串的迭代字符和计数在两个字符串的“0”的出现次数。如果在任何时候在字符串S的“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


Javascript


输出:
YES
NO

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live