给定大小为N 的循环二进制字符串S ,任务是计算需要删除的连续0的最小数量,以便该字符串仅包含1 。
A circular string is a string whose first and last characters are considered to be adjacent to each other.
例子:
Input: S = “11010001”
Output: 2
Explanation:
Remove the substring {S[2]}. Now, the string modifies to “1110001”.
Remove the substring {S[3], …, S[5]} of consecutive 0s. Now, the string modifies to “1111”.
Therefore, the minimum count of removals required is 2.
Input: S = “00110000”
Output: 1
方法:解决给定问题的想法是遍历给定的字符串并计算具有相同数量0的子字符串的数量,例如C 。现在,如果字符串的第一个和最后一个字符是‘0’ ,则打印(C – 1)的值作为所需的最小删除次数。否则,打印C的值作为结果。
注意:如果给定的字符串包含全0 ,则所需的最小删除次数为1 。单独考虑这种情况。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count minimum number of
// removal of consecutive 0s required to
// make binary string consists only of 1s
int minRemovals(string str, int N)
{
// Stores the count of removals
int ans = 0;
bool X = false;
// Traverse the string S
for (int i = 0; i < N; i++) {
// If the current character is '0'
if (str[i] == '0') {
ans++;
// Traverse until consecutive
// characters are only '0's
while (str[i] == '0') {
i++;
}
}
else {
X = true;
}
}
// If the binary string only
// contains 1s, then return 1
if (!X)
return 1;
// If the first and the last
// characters are 0
if (str[0] == '0'
and str[N - 1] == '0') {
ans--;
}
// Return the resultant count
return ans;
}
// Driver Code
int main()
{
string S = "11010001";
int N = S.size();
cout << minRemovals(S, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to count minimum number of
// removal of consecutive 0s required to
// make binary string consists only of 1s
static int minRemovals(String str, int N)
{
// Stores the count of removals
int ans = 0;
boolean X = false;
// Traverse the string S
for(int i = 0; i < N; i++)
{
// If the current character is '0'
if (str.charAt(i) == '0')
{
ans++;
// Traverse until consecutive
// characters are only '0's
while (i < N && str.charAt(i) == '0')
{
i++;
}
}
else
{
X = true;
}
}
// If the binary string only
// contains 1s, then return 1
if (!X)
return 1;
// If the first and the last
// characters are 0
if (str.charAt(0) == '0' &&
str.charAt(N - 1) == '0')
{
ans--;
}
// Return the resultant count
return ans;
}
// Driver Code
public static void main(String[] args)
{
String S = "11010001";
int N = S.length();
System.out.println(minRemovals(S, N));
}
}
// This code is contributed by Kingash
Python3
# Python3 program for the above approach
# Function to count minimum number of
# removal of consecutive 0s required to
# make binary string consists only of 1s
def minRemovals(str, N):
# Stores the count of removals
ans = 0
X = False
# Traverse the string S
i = 0
while i < N:
# If the current character is '0'
if (str[i] == '0'):
ans += 1
# Traverse until consecutive
# characters are only '0's
while (str[i] == '0'):
i += 1
else:
X = True
i += 1
# If the binary string only
# contains 1s, then return 1
if (not X):
return 1
# If the first and the last
# characters are 0
if (str[0] == '0' and str[N - 1] == '0'):
ans -= 1
# Return the resultant count
return ans
# Driver Code
S = "11010001"
N = len(S)
print(minRemovals(S, N))
# This code is contributed by rohan07
C#
// C# program for the above approach
using System;
class GFG {
// Function to count minimum number of
// removal of consecutive 0s required to
// make binary string consists only of 1s
static int minRemovals(string str, int N)
{
// Stores the count of removals
int ans = 0;
bool X = false;
// Traverse the string S
for (int i = 0; i < N; i++) {
// If the current character is '0'
if (str[i] == '0') {
ans++;
// Traverse until consecutive
// characters are only '0's
while (str[i] == '0') {
i++;
}
}
else {
X = true;
}
}
// If the binary string only
// contains 1s, then return 1
if (!X)
return 1;
// If the first and the last
// characters are 0
if (str[0] == '0' && str[N - 1] == '0') {
ans--;
}
// Return the resultant count
return ans;
}
// Driver Code
public static void Main()
{
string S = "11010001";
int N = S.Length;
Console.Write(minRemovals(S, N));
}
}
// This code is contributed by subham348.
Javascript
2
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live