给定一个二进制字符串S ,任务是找到修改字符串所需的最小翻转次数,使其不包含任何连续的0对。
例子:
Input: S = “10001”
Output: 1
Explanation:
Flipping S[2] modifies S to “10101”.
Therefore, the required output is 1.
Input: S = “100001”
Output: 2
Explanation:
Flipping S[1] modifies S to “110001”.
Flipping S[3] modifies S to “110101”.
方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:
- 遍历字符串的字符。对于每个第i个字符,检查S[i]和S[i + 1]是否等于‘0’ 。如果发现为真,则增加 count 并将S[i + 1]更新为‘1’ 。
- 最后,打印获得的计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum flips required
// such that a string does not contain
// any pair of consecutive 0s
bool cntMinOperation(string S, int N)
{
// Stores minimum count of flips
int cntOp = 0;
// Iterate over the characters
// of the string
for (int i = 0; i < N - 1; i++) {
// If two consecutive characters
// are equal to '0'
if (S[i] == '0' && S[i + 1] == '0') {
// Update S[i + 1]
S[i + 1] = '1';
// Update cntOp
cntOp += 1;
}
}
return cntOp;
}
// Driver Code
int main()
{
string S = "10001";
int N = S.length();
cout << cntMinOperation(S, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find minimum flips required
// such that a String does not contain
// any pair of consecutive 0s
static int cntMinOperation(char []S, int N)
{
// Stores minimum count of flips
int cntOp = 0;
// Iterate over the characters
// of the String
for (int i = 0; i < N - 1; i++)
{
// If two consecutive characters
// are equal to '0'
if (S[i] == '0' && S[i + 1] == '0')
{
// Update S[i + 1]
S[i + 1] = '1';
// Update cntOp
cntOp += 1;
}
}
return cntOp;
}
// Driver Code
public static void main(String[] args)
{
String S = "10001";
int N = S.length();
System.out.print(cntMinOperation(S.toCharArray(), N));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# Function to find minimum flips required
# such that a string does not contain
# any pair of consecutive 0s
def cntMinOperation(S, N):
# Stores minimum count of flips
cntOp = 0
# Iterate over the characters
# of the string
for i in range(N - 1):
# If two consecutive characters
# are equal to '0'
if (S[i] == '0' and S[i + 1] == '0'):
# Update S[i + 1]
S[i + 1] = '1'
# Update cntOp
cntOp += 1
return cntOp
# Driver Code
if __name__ == '__main__':
S = "10001"
N = len(S)
print(cntMinOperation([i for i in S], N))
# 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
// such that a String does not contain
// any pair of consecutive 0s
static int cntMinOperation(char []S, int N)
{
// Stores minimum count of flips
int cntOp = 0;
// Iterate over the characters
// of the String
for (int i = 0; i < N - 1; i++)
{
// If two consecutive characters
// are equal to '0'
if (S[i] == '0' && S[i + 1] == '0')
{
// Update S[i + 1]
S[i + 1] = '1';
// Update cntOp
cntOp += 1;
}
}
return cntOp;
}
// Driver Code
public static void Main(string[] args)
{
string S = "10001";
int N = S.Length;
Console.WriteLine(cntMinOperation(S.ToCharArray(), N));
}
}
// This code is contributed by AnkThon
输出:
1
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live