📌  相关文章
📜  旋转的最小翻转次数,以使二进制字符串交替

📅  最后修改于: 2021-04-24 05:22:38             🧑  作者: Mango

给定二进制字符串S0s1s 。任务是通过使用以下操作,使给定的字符串成为交替字符序列:

  • 从开头删除一些前缀,并将其附加到末尾。
  • 翻转给定字符串中的某些或每一位。

打印要翻转以使给定字符串交替的最少位数。

例子:

天真的方法:天真的方法是采用所有N种可能的组合,并计算最小位数以翻转这些字符串中的每一个。在所有此类组合中打印最小数量。
时间复杂度: O(N 2 ),其中N是字符串的长度。
辅助空间: O(N)

高效的方法:可以通过观察最终字符串为“ 101010…”“ 010101…”类型来解决此问题,以便所有1要么位于奇数位置,要么位于偶数位置。请按照以下步骤解决问题:

  1. 创建一个前缀和数组,其中pref [i]表示在索引i之前需要进行的许多更改。
  2. 为上述两种模式创建前缀数组。
  3. 检查每个i ,如果最后添加了substring [0,i],则需要翻转多少个字符。
  4. 在上述步骤中,在所有子字符串中打印最少的翻转次数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function that finds the minimum
// number of flips to make the
// binary string alternating if
// left circular rotation is allowed
int MinimumFlips(string s, int n)
{
    int a[n];
 
    for(int i = 0; i < n; i++)
    {
        a[i] = (s[i] == '1' ? 1 : 0);
    }
 
    // Initialize prefix arrays to store
    // number of changes required to put
    // 1s at either even or odd position
    int oddone[n + 1];
    int evenone[n + 1];
 
    oddone[0] = 0;
    evenone[0] = 0;
 
    for(int i = 0; i < n; i++)
    {
         
        // If i is odd
        if (i % 2 != 0)
        {
             
            // Update the oddone
            // and evenone count
            oddone[i + 1] = oddone[i] +
                                (a[i] == 1 ? 1 : 0);
            evenone[i + 1] = evenone[i] +
                                  (a[i] == 0 ? 1 : 0);
        }
 
        // Else i is even
        else
        {
             
            // Update the oddone
            // and evenone count
            oddone[i + 1] = oddone[i] +
                                (a[i] == 0 ? 1 : 0);
            evenone[i + 1] = evenone[i] +
                                  (a[i] == 1 ? 1 : 0);
        }
    }
 
    // Initialize minimum flips
    int minimum = min(oddone[n], evenone[n]);
 
    // Check if substring[0, i] is
    // appended at end how many
    // changes will be required
    for(int i = 0; i < n; i++)
    {
        if (n % 2 != 0)
        {
            minimum = min(minimum,
                          oddone[n] -
                          oddone[i + 1] +
                         evenone[i + 1]);
            minimum = min(minimum,
                          evenone[n] -
                          evenone[i + 1] +
                           oddone[i + 1]);
        }
    }
 
    // Return minimum flips
    return minimum;
}
 
// Driver Code
int main()
{
     
    // Given String
    string S = "000001100";
 
    // Length of given string
    int n = S.length();
 
    // Function call
    cout << (MinimumFlips(S, n));
}
 
// This code is contributed by chitranayal


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function that finds the minimum
    // number of flips to make the
    // binary string alternating if
    // left circular rotation is allowed
    static int MinimumFlips(String s, int n)
    {
        int[] a = new int[n];
 
        for (int i = 0; i < n; i++) {
            a[i] = (s.charAt(i) == '1' ? 1 : 0);
        }
 
        // Initialize prefix arrays to store
        // number of changes required to put
        // 1s at either even or odd position
        int[] oddone = new int[n + 1];
        int[] evenone = new int[n + 1];
 
        oddone[0] = 0;
        evenone[0] = 0;
 
        for (int i = 0; i < n; i++) {
 
            // If i is odd
            if (i % 2 != 0) {
 
                // Update the oddone
                // and evenone count
                oddone[i + 1]
                    = oddone[i]
                      + (a[i] == 1 ? 1 : 0);
                evenone[i + 1]
                    = evenone[i]
                      + (a[i] == 0 ? 1 : 0);
            }
 
            // Else i is even
            else {
 
                // Update the oddone
                // and evenone count
                oddone[i + 1]
                    = oddone[i]
                      + (a[i] == 0 ? 1 : 0);
                evenone[i + 1]
                    = evenone[i]
                      + (a[i] == 1 ? 1 : 0);
            }
        }
 
        // Initialize minimum flips
        int minimum = Math.min(oddone[n],
                               evenone[n]);
 
        // Check if substring[0, i] is
        // appended at end how many
        // changes will be required
        for (int i = 0; i < n; i++) {
            if (n % 2 != 0) {
                minimum = Math.min(minimum,
                                   oddone[n]
                                       - oddone[i + 1]
                                       + evenone[i + 1]);
                minimum = Math.min(minimum,
                                   evenone[n]
                                       - evenone[i + 1]
                                       + oddone[i + 1]);
            }
        }
 
        // Return minimum flips
        return minimum;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given String
        String S = "000001100";
 
        // Length of given string
        int n = S.length();
 
        // Function call
        System.out.print(MinimumFlips(S, n));
    }
}


Python3
# Python3 program for the above approach
 
# Function that finds the minimum
# number of flips to make the
# binary string alternating if
# left circular rotation is allowed
def MinimumFlips(s, n):
 
    a = [0] * n
 
    for i in range(n):
        a[i] = 1 if s[i] == '1' else 0
 
    # Initialize prefix arrays to store
    # number of changes required to put
    # 1s at either even or odd position
    oddone = [0] * (n + 1)
    evenone = [0] * (n + 1)
 
    for i in range(n):
 
        # If i is odd
        if(i % 2 != 0):
 
            # Update the oddone
            # and evenone count
            if(a[i] == 1):
                oddone[i + 1] = oddone[i] + 1
            else:
                oddone[i + 1] = oddone[i] + 0
 
            if(a[i] == 0):
                evenone[i + 1] = evenone[i] + 1
            else:
                evenone[i + 1] = evenone[i] + 0
 
        # Else i is even
        else:
 
            # Update the oddone
            # and evenone count
            if (a[i] == 0):
                oddone[i + 1] = oddone[i] + 1
            else:
                oddone[i + 1] = oddone[i] + 0
 
            if (a[i] == 1):
                evenone[i + 1] = evenone[i] + 1
            else:
                evenone[i + 1] = evenone[i] + 0
 
    # Initialize minimum flips
    minimum = min(oddone[n], evenone[n])
 
    # Check if substring[0, i] is
    # appended at end how many
    # changes will be required
    for i in range(n):
        if(n % 2 != 0):
            minimum = min(minimum,
                          oddone[n] -
                          oddone[i + 1] +
                         evenone[i + 1])
 
            minimum = min(minimum,
                          evenone[n] -
                          evenone[i + 1] +
                           oddone[i + 1])
 
    # Return minimum flips
    return minimum
 
# Driver Code
 
# Given String
S = "000001100"
 
# Length of given string
n = len(S)
 
# Function call
print(MinimumFlips(S, n))
 
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
class GFG{
 
  // Function that finds the minimum
  // number of flips to make the
  // binary string alternating if
  // left circular rotation is allowed
  static int MinimumFlips(String s, int n)
  {
    int[] a = new int[n];
 
    for (int i = 0; i < n; i++)
    {
      a[i] = (s[i] == '1' ? 1 : 0);
    }
 
    // Initialize prefix arrays to store
    // number of changes required to put
    // 1s at either even or odd position
    int[] oddone = new int[n + 1];
    int[] evenone = new int[n + 1];
 
    oddone[0] = 0;
    evenone[0] = 0;
 
    for (int i = 0; i < n; i++)
    {
 
      // If i is odd
      if (i % 2 != 0)
      {
 
        // Update the oddone
        // and evenone count
        oddone[i + 1] = oddone[i] +
                         (a[i] == 1 ? 1 : 0);
        evenone[i + 1] = evenone[i] +
                          (a[i] == 0 ? 1 : 0);
      }
 
      // Else i is even
      else
      {
 
        // Update the oddone
        // and evenone count
        oddone[i + 1] = oddone[i] +
                         (a[i] == 0 ? 1 : 0);
        evenone[i + 1] = evenone[i] +
                          (a[i] == 1 ? 1 : 0);
      }
    }
 
    // Initialize minimum flips
    int minimum = Math.Min(oddone[n],
                           evenone[n]);
 
    // Check if substring[0, i] is
    // appended at end how many
    // changes will be required
    for (int i = 0; i < n; i++)
    {
      if (n % 2 != 0)
      {
        minimum = Math.Min(minimum, oddone[n] -
                                       oddone[i + 1] +
                                    evenone[i + 1]);
        minimum = Math.Min(minimum, evenone[n] -
                                       evenone[i + 1] +
                                       oddone[i + 1]);
      }
    }
 
    // Return minimum flips
    return minimum;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    // Given String
    String S = "000001100";
 
    // Length of given string
    int n = S.Length;
 
    // Function call
    Console.Write(MinimumFlips(S, n));
  }
}
 
// This code is contributed by Rajput-Ji


输出:
3


时间复杂度: O(N),其中N是给定字符串的长度。
辅助空间: O(N)