📌  相关文章
📜  形成给定二进制字符串所需的最少翻转

📅  最后修改于: 2021-04-27 22:49:20             🧑  作者: Mango

给定字符串S ,任务是查找将仅包含零的初始二进制字符串转换为S所需的最小翻转,其中字符的每次翻转也将翻转所有后续字符。
例子:

方法:
为了解决问题,请按照以下步骤操作:

  • 最初将curr存储为“ 1”
  • 遍历S并找到curr的第一个匹配项。遇到curr时增加计数。如果curr‘1 ‘,则存储‘0’ ,反之亦然。
  • 对S的整个遍历重复上述步骤。
  • 计数的最终值给出了所需的答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to return the count
// of minimum flips required
int minFlips(string target)
{
    char curr = '1';
    int count = 0;
    for(int i = 0; i < target.length(); i++)
    {
         
       // If curr occurs in the final string
       if (target[i] == curr)
       {
           count++;
            
           // Switch curr to '0' if '1'
           // or vice-versa
           curr = (char)(48 + (curr + 1) % 2);
       }
    }
    return count;
}
 
// Driver Code
int main()
{
    string S = "011000";
     
    cout << (minFlips(S));
}
 
// This code is contributed by rock_cool


Java
// Java program for the above approach
import java.util.Arrays;
 
public class GFG {
    // Function to return the count of
    // minimum flips required
    public static int minFlips(String target)
    {
 
        char curr = '1';
        int count = 0;
        for (int i = 0; i < target.length(); i++) {
 
            // If curr occurs in the final string
            if (target.charAt(i) == curr) {
 
                count++;
 
                // Switch curr to '0' if '1'
                // or vice-versa
                curr = (char)(48 + (curr + 1) % 2);
            }
        }
 
        return count;
    }
 
    // Driver Code
    public static void main(String args[])
    {
 
        String S = "011000";
        System.out.println(minFlips(S));
    }
}


Python3
# Python3 program for the above approach
 
# Function to return the count
# of minimum flips required
def minFlips(target):
 
    curr = '1'
    count = 0
     
    for i in range(len(target)):
         
        # If curr occurs in the final string
        if (target[i] == curr):
            count += 1
             
            # Switch curr to '0' if '1'
            # or vice-versa
            curr = chr(48 + (ord(curr) + 1) % 2)
     
    return count
 
# Driver Code
if __name__ == "__main__":
     
    S = "011000"
     
    print(minFlips(S))
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to return the count of
// minimum flips required
public static int minFlips(String target)
{
    char curr = '1';
    int count = 0;
    for(int i = 0; i < target.Length; i++)
    {
         
       // If curr occurs in the readonly string
       if (target[i] == curr)
       {
           count++;
            
           // Switch curr to '0' if '1'
           // or vice-versa
           curr = (char)(48 + (curr + 1) % 2);
       }
    }
    return count;
}
 
// Driver code
public static void Main(String []args)
{
    String S = "011000";
    Console.WriteLine(minFlips(S));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2

时间复杂度: O(N)