给定一个偶数长度的二进制字符串str ,由相等数量的0和1 组成,任务是通过重复反转子字符串将所有 1 和 0 分成单独的两半。打印所需的最小反转计数。
例子:
Input: str = “01011100”
Output: 2
Explanation: The operations performed are as follows:
- “01011100″ -> “11101000”
- “11101000″ -> “11110000”
Input: str = “101010”
Output: 2
Explanation: The operations performed are as follows:
- “101010″ -> “110100”
- “110100″ -> “111000”
方法:思路是统计字符串中任意两个连续字符不相等的实例数。请按照以下步骤解决问题:
- 初始化一个变量,比如ans ,以计算相邻不等字符对的数量。
- 现在,在反转任何子字符串后,计数减少2 。
- 如果ans的值是奇数,那么所需的答案将是(ans – 1)/2,因为最终字符串将在字符串的中心包含一对这样的对。
- 否则,所需的答案是ans/2 。
下面是上述方法的实现:
C++14
#include
using namespace std;
// Function to count the minimum number
// of operations required to segregate
// all 1s and 0s in a binary string
void minOps(string s, int N)
{
// Stores the count of unequal
// pair of adjacent characters
int ans = 0;
for (int i = 1; i < N; i++)
{
// If an unequal pair of
// adjacent characters occurs
if (s[i] != s[i - 1])
{
ans++;
}
}
// For odd count
if (ans % 2 == 1)
{
cout << (ans - 1) / 2 << endl;
return;
}
// For even count
cout<<(ans / 2);
}
// Driver Code
int main()
{
// Given string
string str = "01011100";
// Length of the string
int N = str.size();
// Prints the minimum count
// of operations required
minOps(str, N);
return 0;
}
// This code is contributed by mohit kumar 29
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to count the minimum number
// of operations required to segregate
// all 1s and 0s in a binary string
static void minOps(String s, int N)
{
// Stores the count of unequal
// pair of adjacent characters
int ans = 0;
for (int i = 1; i < N; i++) {
// If an unequal pair of
// adjacent characters occurs
if (s.charAt(i) != s.charAt(i - 1)) {
ans++;
}
}
// For odd count
if (ans % 2 == 1) {
System.out.print((ans - 1) / 2);
return;
}
// For even count
System.out.print(ans / 2);
}
// Driver Code
public static void main(String[] args)
{
// Given string
String str = "01011100";
// Length of the string
int N = str.length();
// Prints the minimum count
// of operations required
minOps(str, N);
}
}
Python3
# Python 3 implementation of above approach
# Function to count the minimum number
# of operations required to segregate
# all 1s and 0s in a binary string
def minOps(s, N) :
# Stores the count of unequal
# pair of adjacent characters
ans = 0
for i in range(1, N):
# If an unequal pair of
# adjacent characters occurs
if (s[i] != s[i - 1]) :
ans += 1
# For odd count
if (ans % 2 == 1) :
print((ans - 1) // 2)
return
# For even count
print(ans // 2)
# Driver Code
# Given string
str = "01011100"
# Length of the string
N = len(str)
# Prints the minimum count
# of operations required
minOps(str, N)
# This code is contributed by sanjoy_62.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to count the minimum number
// of operations required to segregate
// all 1s and 0s in a binary string
static void minOps(String s, int N)
{
// Stores the count of unequal
// pair of adjacent characters
int ans = 0;
for (int i = 1; i < N; i++)
{
// If an unequal pair of
// adjacent characters occurs
if (s[i] != s[i - 1])
{
ans++;
}
}
// For odd count
if (ans % 2 == 1)
{
Console.Write((ans - 1) / 2);
return;
}
// For even count
Console.Write(ans / 2);
}
// Driver Code
public static void Main(String[] args)
{
// Given string
String str = "01011100";
// Length of the string
int N = str.Length;
// Prints the minimum count
// of operations required
minOps(str, N);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
2
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。