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

📅  最后修改于: 2021-04-24 16:25:58             🧑  作者: Mango

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

例子:

天真的方法:最简单的方法是遍历给定的字符串 。对于找到的字符串的所有字符均为’0’,请遍历其右边以查找与当前字符不相邻的下一个’ 0 ‘。翻转两个字符并将答案增加1。如果右侧不存在“ 0”,则翻转当前字符并将答案增加1。

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

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

  • 初始化两个变量以使用-1维护字符串中先前找到的0 s的索引。
  • 遍历字符串并检查最后找到的两个索引是否不相邻,然后将计数器加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


输出:
2






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