删除给定二进制字符串中任何连续 3 个 0 或 1 的最小翻转次数
给定一个由N个字符组成的二进制字符串S ,任务是找到所需的最小翻转次数,使得不存在三个连续的相同字符。
例子:
Input: S = “1100011”
Output: 1
Explanation:
Flip the character at index 3 modifies the string S “1101011” that have no three consecutive same characters. Therefore, the minimum number of flips required is 1.
Input: S = “0001111101”
Output: 2
方法:给定的问题可以通过考虑每三个连续字符来解决,如果它们相同,则增加所需的翻转次数,因为需要翻转三个字符之一。请按照以下步骤解决问题:
- 初始化变量,例如count为0 ,存储所需的最小翻转次数。
- 如果字符串的大小小于等于2,则返回0 ,因为不需要任何翻转。
- 使用变量i迭代范围[0, N – 2)并执行以下步骤:
- 如果索引i 、 (i + 1)和(i + 2)字符处的字符相同,则将count的值增加1并将i的值增加3 。
- 否则,将i的值增加1 。
- 执行上述步骤后,打印count的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of flips to make all three pairs of
// consecutive characters different
int minFlips(string str)
{
// Stores resultant count of pairs
int count = 0;
// Base Case
if (str.size() <= 2) {
return 0;
}
// Iterate over the range [0, N - 2]
for (int i = 0; i < str.size() - 2;) {
// If the consecutive 3 numbers
// are the same then increment
// the count and the counter
if (str[i] == str[i + 1]
&& str[i + 2] == str[i + 1]) {
i = i + 3;
count++;
}
else {
i++;
}
}
// Return the answer
return count;
}
// Driver Code
int main()
{
string S = "0011101";
cout << minFlips(S);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find the minimum number
// of flips to make all three pairs of
// consecutive characters different
static int minFlips(String str)
{
// Stores resultant count of pairs
int count = 0;
// Base Case
if (str.length() <= 2) {
return 0;
}
// Iterate over the range [0, N - 2]
for (int i = 0; i < str.length() - 2😉 {
// If the consecutive 3 numbers
// are the same then increment
// the count and the counter
if (str.charAt(i) == str.charAt(i+1)
&& str.charAt(i+2) == str.charAt(i+1)) {
i = i + 3;
count++;
}
else {
i++;
}
}
// Return the answer
return count;
}
// Driver Code
public static void main(String[] args)
{
String S = "0011101";
System.out.println(minFlips(S));
}
}
// This code is contributed by dwivediyash
Python3
# python 3 program for the above approach
#
# Function to find the minimum number
# of flips to make all three pairs of
# consecutive characters different
def minFlips(st):
# Stores resultant count of pairs
count = 0
# Base Case
if (len(st) <= 2):
return 0
# Iterate over the range [0, N - 2]
for i in range(len(st) - 2):
# If the consecutive 3 numbers
# are the same then increment
# the count and the counter
if (st[i] == st[i + 1]
and st[i + 2] == st[i + 1]):
i = i + 3
count += 1
else:
i += 1
# Return the answer
return count
# Driver Code
if __name__ == "__main__":
S = "0011101"
print(minFlips(S))
# This code is contributed by ukasp.
Javascript
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the minimum number
// of flips to make all three pairs of
// consecutive characters different
static int minFlips(string str)
{
// Stores resultant count of pairs
int count = 0;
// Base Case
if (str.Length <= 2) {
return 0;
}
// Iterate over the range [0, N - 2]
for (int i = 0; i < str.Length - 2;) {
// If the consecutive 3 numbers
// are the same then increment
// the count and the counter
if (str[i] == str[i+1]
&& str[i+2] == str[i+1]) {
i = i + 3;
count++;
}
else {
i++;
}
}
// Return the answer
return count;
}
// Driver Code
public static void Main(string[] args)
{
string S = "0011101";
Console.WriteLine(minFlips(S));
}
}
// This code is contributed by AnkThon
输出:
1
时间复杂度: O(N)
辅助空间: O(1)