给定一个长度为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.
Javascript
9
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live