📌  相关文章
📜  形成给定二进制字符串所需的最小翻转次数,其中每次翻转也将所有位更改为右侧

📅  最后修改于: 2021-09-06 06:21:24             🧑  作者: Mango

给定字符串S ,任务是找到将仅由零组成的初始二进制字符串转换为 S 所需的最小翻转,其中每个字符的翻转也会翻转所有后续字符。
例子:

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

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

下面是上述方法的实现。

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)

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