📌  相关文章
📜  将二进制字符串转换为另一个字符串所需的最小子字符串翻转

📅  最后修改于: 2021-04-17 17:37:14             🧑  作者: Mango

给定分别为NM的两个二进制字符串S1S2 ,任务是找到将字符串S1转换为S2所需的相等字符的子字符串的最小反转数目。如果无法将字符串S1转换为S2 ,则打印“ -1”

例子:

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

  • 初始化一个变量,例如answer ,以存储所需的求逆结果。
  • 如果给定的字符串S1S2的长度不同,则打印“ -1”
  • 遍历范围[0,N – 1]并执行以下步骤:
    • 如果S1 [i]S2 [i]不相同,则重复进行直到S1 [i]S2 [i]相同。由于需要翻转当前子字符串,因此将答案增加1
    • 否则,继续进行下一个迭代。
  • 完成上述步骤后,将答案的值打印为所需的子字符串翻转结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the minimum number
// of reversals requred to make the
// given binary strings s1 and s2 same
int canMakeSame(string s1, string s2)
{
    // Stores the minimum count of
    // reversal of substrings required
    int ans = 0;
 
    // If the length of the strings
    // are not the same then return -1
    if (s1.size() != s2.size()) {
        return -1;
    }
 
    int N = s1.length();
 
    // Iterate over each character
    for (int i = 0; i < N; i++) {
 
        // If s1[i] is not
        // equal to s2[i]
        if (s1[i] != s2[i]) {
 
            // Iterate until s1[i] != s2[i]
            while (i < s1.length()
                   && s1[i] != s2[i]) {
                i++;
            }
 
            // Increment answer by 1
            ans++;
        }
    }
 
    // Return the resultant count of
    // reversal of substring required
    return ans;
}
 
// Driver Code
int main()
{
    string S1 = "100001";
    string S2 = "110111";
 
    // Function Call
    cout << canMakeSame(S1, S2);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
 
  // Function to count the minimum number
  // of reversals requred to make the
  // given binary strings s1 and s2 same
  static int canMakeSame(String s1, String s2)
  {
 
    // Stores the minimum count of
    // reversal of substrings required
    int ans = 0;
 
    // If the length of the strings
    // are not the same then return -1
    if (s1.length() != s2.length()) {
      return -1;
    }
 
    int N = s1.length();
 
    // Iterate over each character
    for (int i = 0; i < N; i++)
    {
 
      // If s1[i] is not
      // equal to s2[i]
      if (s1.charAt(i) != s2.charAt(i))
      {
 
        // Iterate until s1[i] != s2[i]
        while (i < s1.length()
               && s1.charAt(i) != s2.charAt(i))
        {
          i++;
        }
 
        // Increment answer by 1
        ans++;
      }
    }
 
    // Return the resultant count of
    // reversal of substring required
    return ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    String S1 = "100001";
    String S2 = "110111";
 
    // Function Call
    System.out.println(canMakeSame(S1, S2));
  }
}
 
// This code is contributed by Dharanendra L V


Python3
# Python3 program for the above approach
 
# Function to count the minimum number
# of reversals requred to make the
# given binary strings s1 and s2 same
def canMakeSame(s1, s2) :
     
    # Stores the minimum count of
    # reversal of substrings required
    ans = -1
 
    # If the length of the strings
    # are not the same then return -1
    if (len(s1) != len(s2)) :
        return -1
    N = len(s1)
 
    # Iterate over each character
    for i in range(0, N):
 
        # If s1[i] is not
        # equal to s2[i]
        if (s1[i] != s2[i]) :
 
            # Iterate until s1[i] != s2[i]
            while (i < len(s1)
                and s1[i] != s2[i]) :
                i += 1
             
            # Increment answer by 1
            ans += 1
     
    # Return the resultant count of
    # reversal of subrequired
    return ans
 
# Driver Code
 
S1 = "100001"
S2 = "110111"
 
# Function Call
print(canMakeSame(S1, S2))
 
# This code is contributed by code_hunt.


C#
// C# program for the above approach
using System;
 
class GFG{
 
  // Function to count the minimum number
  // of reversals requred to make the
  // given binary strings s1 and s2 same
  static int canMakeSame(string s1, string s2)
  {
 
    // Stores the minimum count of
    // reversal of substrings required
    int ans = 0;
 
    // If the length of the strings
    // are not the same then return -1
    if (s1.Length != s2.Length) {
      return -1;
    }
 
    int N = s1.Length;
 
    // Iterate over each character
    for (int i = 0; i < N; i++)
    {
 
      // If s1[i] is not
      // equal to s2[i]
      if (s1[i] != s2[i])
      {
 
        // Iterate until s1[i] != s2[i]
        while (i < s1.Length
               && s1[i] != s2[i])
        {
          i++;
        }
 
        // Increment answer by 1
        ans++;
      }
    }
 
    // Return the resultant count of
    // reversal of substring required
    return ans;
  }
 
// Driver Code
public static void Main(string[] args)
{
    string S1 = "100001";
    string S2 = "110111";
 
    // Function Call
    Console.Write(canMakeSame(S1, S2));
}
}
 
// This code is contributed by susmitakundugoaldanga.


输出:
2

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