📌  相关文章
📜  从二进制字符串中删除所有 0 所需的最小非相邻对翻转

📅  最后修改于: 2021-09-07 02:18:51             🧑  作者: Mango

给定一个二进制字符串S,任务是找到从二进制字符串中删除所有 0 所需的最多翻转两个不相邻字符的最少操作次数。

例子:

朴素的方法:最简单的方法是遍历给定的字符串。对于字符串的所有字符认为是“0”,遍历其找到下一个“0”,这是不相邻的当前字符权。翻转两个字符并将答案加 1。如果右侧没有“0”,则翻转当前字符并将答案加 1。

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

有效的方法:这个想法是存储需要翻转的先前字符的索引。下面是在步骤帮助下的插图:

  • 初始化两个变量以使用-1维护先前在字符串找到的0的索引。
  • 遍历字符串并检查最后两个找到的索引是否不相邻,然后将计数器加 1。此外,更新前两个找到的索引的位置。
  • 最后,在完成遍历后,如果最后找到的两个索引都不是-1 ,则将计数器增加2 。否则,如果最后找到的索引之一不是-1 ,则将计数器1

下面是上述方法的实现:

C++
// C++ program for
// the above approach
#include
using namespace std;
 
// Function to find minimum flips
// required to remove all 0s from
// a given binary string
int minOperation(string s)
{
  // Length of given string
  int n = s.length();
 
  // Stores the indices of
  // previous 0s
  int temp1 = -1, temp2 = -1;
 
  // Stores the minimum operations
  int ans = 0;
 
  // Traverse string to find minimum
  // operations to obtain required string
  for (int i = 0; i < n; i++)
  {
    // Current character
    int curr = s[i];
 
    // If current character is '0'
    if (curr == '0')
    {
      // Update temp1 with current
      // index, if both temp
      // variables are empty
      if (temp1 == -1 && temp2 == -1)
      {
        temp1 = i;
      }
 
      // Update temp2 with current
      // index, if temp1 contains
      // prev index but temp2 is empty
      else if (temp1 != -1 && temp2 == -1 &&
               i - temp1 == 1)
      {
        temp2 = i;
      }
 
      // If temp1 is not empty
      else if (temp1 != -1)
      {
        // Reset temp1 to -1
        temp1 = -1;
 
        // Increase ans
        ans++;
      }
 
      // If temp2 is not empty but
      // temp1 is empty
      else if (temp1 == -1 && temp2 != -1 &&
               i - temp2 != 1)
      {
        // Reset temp2 to -1
        temp2 = -1;
 
        // Increase ans
        ans++;
      }
    }
  }
 
  // If both temp variables
  // are not empty
  if (temp1 != -1 && temp2 != -1)
  {
    ans += 2;
  }
  // Otherwise
  else if (temp1 != -1 || temp2 != -1)
  {
    ans += 1;
  }
 
  // Return the answer
  return ans;
}
 
// Driver Code
int main()
{
  string s = "110010";
  cout << (minOperation(s));
}
 
// This code is contributed by gauravrajput1


Java
// Java program for above approach
 
import java.util.*;
 
class GFG {
 
    // Function to find minimum flips
    // required to remove all 0s from
    // a given binary string
    static int minOperation(String s)
    {
        // Length of given string
        int n = s.length();
 
        // Stores the indices of
        // previous 0s
        int temp1 = -1, temp2 = -1;
 
        // Stores the minimum operations
        int ans = 0;
 
        // Traverse string to find minimum
        // operations to obtain required string
        for (int i = 0; i < n; i++) {
            // Current character
            int curr = s.charAt(i);
 
            // If current character is '0'
            if (curr == '0') {
                // Update temp1 with current
                // index, if both temp
                // variables are empty
                if (temp1 == -1 && temp2 == -1) {
                    temp1 = i;
                }
 
                // Update temp2 with current
                // index, if temp1 contains
                // prev index but temp2 is empty
                else if (temp1 != -1 && temp2 == -1
                         && i - temp1 == 1) {
                    temp2 = i;
                }
 
                // If temp1 is not empty
                else if (temp1 != -1) {
                    // Reset temp1 to -1
                    temp1 = -1;
 
                    // Increase ans
                    ans++;
                }
 
                // If temp2 is not empty but
                // temp1 is empty
                else if (temp1 == -1 && temp2 != -1
                         && i - temp2 != 1) {
                    // Reset temp2 to -1
                    temp2 = -1;
 
                    // Increase ans
                    ans++;
                }
            }
        }
 
        // If both temp variables are not empty
        if (temp1 != -1 && temp2 != -1) {
            ans += 2;
        }
        // Otherwise
        else if (temp1 != -1 || temp2 != -1) {
            ans += 1;
        }
 
        // Return the answer
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        String s = "110010";
        System.out.println(minOperation(s));
    }
}


Python3
# Python3 program for above approach
 
# Function to find minimum flips
# required to remove all 0s from
# a given binary string
def minOperation(s):
 
    # Length of given string
    n = len(s)
 
    # Stores the indices of
    # previous 0s
    temp1 = -1
    temp2 = -1
 
    # Stores the minimum operations
    ans = 0
 
    # Traverse string to find minimum
    # operations to obtain required string
    for i in range(n):
         
        # Current character
        curr = s[i]
 
        # If current character is '0'
        if (curr == '0'):
             
            # Update temp1 with current
            # index, if both temp
            # variables are empty
            if (temp1 == -1 and temp2 == -1):
                temp1 = i
 
            # Update temp2 with current
            # index, if temp1 contains
            # prev index but temp2 is empty
            elif (temp1 != -1 and temp2 == -1 and
                               i - temp1 == 1):
                temp2 = i
                 
            # If temp1 is not empty
            elif (temp1 != -1):
                 
                # Reset temp1 to -1
                temp1 = -1
 
                # Increase ans
                ans += 1
 
            # If temp2 is not empty but
            # temp1 is empty
            elif (temp1 == -1 and temp2 != -1 and
                              i - temp2 != 1):
                                   
                # Reset temp2 to -1
                temp2 = -1
 
                # Increase ans
                ans += 1
                 
    # If both temp variables are not empty
    if (temp1 != -1 and temp2 != -1):
        ans += 2
 
    # Otherwise
    elif (temp1 != -1 or temp2 != -1):
        ans += 1
 
    # Return the answer
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    s = "110010"
     
    print(minOperation(s))
 
# This code is contributed by mohit kumar 29


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function to find minimum flips
// required to remove all 0s from
// a given binary string
static int minOperation(String s)
{
  // Length of given string
  int n = s.Length;
 
  // Stores the indices of
  // previous 0s
  int temp1 = -1, temp2 = -1;
 
  // Stores the minimum operations
  int ans = 0;
 
  // Traverse string to find minimum
  // operations to obtain required string
  for (int i = 0; i < n; i++)
  {
    // Current character
    int curr = s[i];
 
    // If current character is '0'
    if (curr == '0')
    {
      // Update temp1 with current
      // index, if both temp
      // variables are empty
      if (temp1 == -1 && temp2 == -1)
      {
        temp1 = i;
      }
 
      // Update temp2 with current
      // index, if temp1 contains
      // prev index but temp2 is empty
      else if (temp1 != -1 &&
               temp2 == -1 &&
               i - temp1 == 1)
      {
        temp2 = i;
      }
 
      // If temp1 is not empty
      else if (temp1 != -1)
      {
        // Reset temp1 to -1
        temp1 = -1;
 
        // Increase ans
        ans++;
      }
 
      // If temp2 is not empty but
      // temp1 is empty
      else if (temp1 == -1 &&
               temp2 != -1 &&
               i - temp2 != 1)
      {
        // Reset temp2 to -1
        temp2 = -1;
 
        // Increase ans
        ans++;
      }
    }
  }
 
  // If both temp variables
  // are not empty
  if (temp1 != -1 && temp2 != -1)
  {
    ans += 2;
  }
   
  // Otherwise
  else if (temp1 != -1 || temp2 != -1)
  {
    ans += 1;
  }
 
  // Return the answer
  return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
  String s = "110010";
  Console.WriteLine(minOperation(s));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
2

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

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