给定长度为N的二进制字符串S ,任务是根据以下条件找到需要插入的最小字符数,以使字符串中的所有字符都变为相同:
If ‘1’ is inserted into the string, then all the ‘0’s nearest to the inserted ‘1’ is flipped or vice-versa.
例子:
Input: S = “11100”
Output: 1
Explanation:
Operation 1: Inserting ‘1’ at the last of the given string modifies S to “111001”. Adding ‘1’ to the last flips all the nearest ‘0’s to the inserted ‘1’. Therefore, the resultant string is “111111”.
After completing the above operation, all the characters of the string are the same. Therefore count of operations is 1.
Input: S = “0101010101”
Output: 9
方法:想法是基于以下观察结果,通过贪婪方法解决此问题:
- 可以看出,在此操作中,将“ 1”或“ 0”的一个连续部分取反会使部分的数目减少一。因此,重复此操作以将其全部分割为一个部分就足够了。所需的操作数等于-1的部分。
- 用更简单的术语来说,计算不相等的相邻字符对的总数,以便反转其中一个可以将整个子字符串转换为相似的子字符串。
请按照以下步骤解决问题:
- 初始化一个变量,例如count ,该变量存储不同相邻字符的计数。
- 遍历字符串并检查当前字符和下一个字符是否不同,然后增加count的值。
- 完成上述步骤后,将count的值打印为所需的最少操作。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to calculate the minimum
// number of operations required to make
// all characters of the string same
int minOperations(string& S)
{
// Stores count of operations
int count = 0;
// Traverse the string
for (int i = 1; i < S.length(); i++) {
// Check if adjacent
// characters are same or not
if (S[i] != S[i - 1]) {
// Increment count
count += 1;
}
}
// Print the count obtained
cout << count;
}
// Driver Code
int main()
{
string S = "0101010101";
minOperations(S);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to calculate the minimum
// number of operations required to make
// all characters of the string same
static void minOperations(String S)
{
// Stores count of operations
int count = 0;
// Traverse the string
for (int i = 1; i < S.length(); i++)
{
// Check if adjacent
// characters are same or not
if (S.charAt(i) != S.charAt(i - 1))
{
// Increment count
count += 1;
}
}
// Print the count obtained
System.out.print(count);
}
// Driver Code
public static void main(String[] args)
{
String S = "0101010101";
minOperations(S);
}
}
// This code is contributed by susmitakundugoaldanga.
Python3
# Python program to implement
# the above approach
# Function to calculate the minimum
# number of operations required to make
# all characters of the string same
def minOperations(S):
# Stores count of operations
count = 0;
# Traverse the string
for i in range(1, len(S)):
# Check if adjacent
# characters are same or not
if (S[i] != S[i - 1]):
# Increment count
count += 1;
# Prthe count obtained
print(count);
# Driver Code
if __name__ == '__main__':
S = "0101010101";
minOperations(S);
# This code is contributed by 29AjayKumar
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to calculate the minimum
// number of operations required to make
// all characters of the string same
static void minOperations(string S)
{
// Stores count of operations
int count = 0;
// Traverse the string
for (int i = 1; i < S.Length; i++)
{
// Check if adjacent
// characters are same or not
if (S[i] != S[i - 1])
{
// Increment count
count += 1;
}
}
// Print the count obtained
Console.Write(count);
}
// Driver Code
public static void Main()
{
string S = "0101010101";
minOperations(S);
}
}
// This code is contributed by code_hunt.
输出:
9
时间复杂度: O(N)
辅助空间: O(1)