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

📅  最后修改于: 2021-10-25 06:23:59             🧑  作者: Mango

给定两个二进制字符串AB ,任务是找到从 A 的第一个字符开始的子字符串需要翻转的最小次数,即将1 s 转换为0 s 和0 s 到1 s,以将A转换为

例子:

的方法:我们的想法是初始化,保持最后索引在该字符在A是从在B点字符不同的变量。然后将 A从第一个索引到最后一个索引取反。重复直到两个字符串相等。请按照以下步骤解决问题:

  • 初始化一个变量last_index ,该变量保存AB 中字符不同的最后一个索引。
  • 从第1 个索引last_index 否定字符串A并增加步数。
  • 重复上述步骤,直到字符串A变得等于字符串B
  • 执行操作后,在两个字符串相同后打印步数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function that finds the minimum
// number of operations required such
// that string A and B are the same
void findMinimumOperations(string a,
                           string b)
{
 
    // Stores the count of steps
    int step = 0;
 
    // Stores the last index whose
    // bits are not same
    int last_index;
 
    // Iterate until both string
    // are unequal
    while (a != b) {
 
        // Check till end of string to
        // find rightmost unequals bit
        for (int i = 0;
             i < a.length(); i++) {
 
            // Update the last index
            if (a[i] != b[i]) {
                last_index = i;
            }
        }
 
        // Flipping characters up
        // to the last index
        for (int i = 0;
             i <= last_index; i++) {
 
            // Flip the bit
            a[i] = (a[i] == '0') ? '1' : '0';
        }
 
        // Increasing steps by one
        step++;
    }
 
    // Print the count of steps
    cout << step;
}
 
// Driver Code
int main()
{
    // Given strings A and B
    string A = "101010", B = "110011";
 
    // Function Call
    findMinimumOperations(A, B);
 
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
 
// Function that finds the minimum
// number of operations required such
// that String A and B are the same
static void findMinimumOperations(char[] a,
                                  char[] b)
{
  // Stores the count of steps
  int step = 0;
 
  // Stores the last index whose
  // bits are not same
  int last_index = 0;
 
  // Iterate until both String
  // are unequal
  while (!Arrays.equals(a, b))
  {
    // Check till end of String to
    // find rightmost unequals bit
    for (int i = 0;
             i < a.length; i++)
    {
      // Update the last index
      if (a[i] != b[i])
      {
        last_index = i;
      }
    }
 
    // Flipping characters up
    // to the last index
    for (int i = 0;
             i <= last_index; i++)
    {
 
      // Flip the bit
      a[i] = (a[i] == '0') ?
              '1' : '0';
    }
    // Increasing steps by one
    step++;
  }
 
  // Print the count of steps
  System.out.print(step);
}
 
// Driver Code
public static void main(String[] args)
{
    // Given Strings A and B
    String A = "101010",
           B = "110011";
 
    // Function Call
    findMinimumOperations(A.toCharArray(),
                          B.toCharArray());
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function that finds the minimum
# number of operations required such
# that string A and B are the same
def findMinimumOperations(a, b):
     
    # Stores the count of steps
    step = 0
 
    # Stores the last index whose
    # bits are not same
    last_index = 0
 
    # Iterate until both string
    # are unequal
    while (a != b):
        a = [i for i in a]
         
        # Check till end of string to
        # find rightmost unequals bit
        for i in range(len(a)):
             
            # Update the last index
            if (a[i] != b[i]):
                last_index = i
 
        # Flipping characters up
        # to the last index
        for i in range(last_index + 1):
             
            # Flip the bit
            if (a[i] == '0'):
                a[i] = '1'
            else:
                a[i] = '0'
                 
        a = "".join(a)       
 
        # Increasing steps by one
        step += 1
 
    # Print the count of steps
    print(step)
 
# Driver Code
if __name__ == '__main__':
     
    # Given strings A and B
    A = "101010"
    B = "110011"
 
    # Function Call
    findMinimumOperations(A, B)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
 
class GFG{
 
// Function that finds the minimum
// number of operations required such
// that string A and B are the same
static void findMinimumOperations(string a,
                                  string b)
{
   
  // Stores the count of steps
  int step = 0;
 
  // Stores the last index whose
  // bits are not same
  int last_index = 0;
 
  // Iterate until both string
  // are unequal
  while (a.Equals(b) == false)
  {
     
    // Check till end of string to
    // find rightmost unequals bit
    for(int i = 0; i < a.Length; i++)
    {
       
      // Update the last index
      if (a[i] != b[i])
      {
        last_index = i;
      }
    }
 
    // Flipping characters up
    // to the last index
    char[] ch = a.ToCharArray();
    for(int i = 0;
            i <= last_index; i++)
    {
       
      // Flip the bit
      if (ch[i] == '0')
      {
        ch[i] = '1';
      }
      else
      {
        ch[i] = '0';
      }
    }
    a = new string(ch);
     
    // Increasing steps by one
    step++;
  }
 
  // Print the count of steps
  Console.WriteLine(step);
}
 
// Driver Code
public static void Main()
{
   
  // Given strings A and B
  string A = "101010";
  string B = "110011";
 
  // Function Call
  findMinimumOperations(A,B);
}
}
 
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出:
4

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程