给定大小为N的二进制字符串S (其中N为偶数),任务是通过以1 个单位为代价翻转相邻的不同字符之一或交换索引处的字符来找到使给定二进制字符串平衡的最小成本i和j使得(i < j)以(j – i) 单位为代价。如果无法使字符串平衡,则打印“-1” 。
A binary string is balanced if it can be reduced to an empty string by deleting two consecutive alternating characters at once and then concatenating the remaining characters.
例子:
Input: S = “110110”
Output: 1
Explanation: Following operations are performed:
Operation 1: As the adjacent characters at indices 0 and 1 are different, flipping S[0] modifies S to “010110”. Cost = 1.
After completing the above operations, the given string becomes balanced, as it can be reduced to an empty string by deleting two consecutive alternating characters.
Therefore, the total cost is 1.
Input: S = “11100”
Output: -1
方法:根据观察,相邻不同字符的翻转比使用字符交换更优化,并且字符串中0和1的位置无关紧要,可以解决给定的问题。请按照以下步骤解决问题:
- 如果所有字符都相等,则无法使字符串平衡。因此,打印“-1” 。
- 将 1 和 0 的计数存储在变量中,分别说count1和count0 。
- 将count1和count0的绝对差值存储在变量K 中。
- 现在,翻转K/2字符以使字符串平衡并打印K/2的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum cost
// to convert the given string into
// balanced string
void findMinimumCost(string s, int N)
{
// Stores count of 1's and 0's in
// the string
int count_1 = 0, count_0 = 0;
// Traverse the string
for (int i = 0; i < N; i++) {
if (s[i] == '1')
// Increment count1
count_1++;
else
// Increment count 0
count_0++;
}
// Stores absolute difference of
// counts of 0's and 1's
int k = abs(count_0 - count_1);
// If string consists of only
// 0's and 1's
if (count_1 == N || count_0 == N)
cout << -1 << endl;
// Print minimum cost
else
cout << k / 2 << endl;
}
// Driver Code
int main()
{
string S = "110110";
int N = S.length();
findMinimumCost(S, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the minimum cost
// to convert the given string into
// balanced string
static void findMinimumCost(String s, int N)
{
// Stores count of 1's and 0's in
// the string
int count_1 = 0, count_0 = 0;
// Traverse the string
for (int i = 0; i < N; i++) {
if (s.charAt(i) == '1')
// Increment count1
count_1++;
else
// Increment count 0
count_0++;
}
// Stores absolute difference of
// counts of 0's and 1's
int k = Math.abs(count_0 - count_1);
// If string consists of only
// 0's and 1's
if (count_1 == N || count_0 == N)
System.out.println( -1);
// Print minimum cost
else
System.out.println( k / 2);
}
// Driver Code
public static void main(String[] args)
{
String S = "110110";
int N = S.length();
findMinimumCost(S, N);
}
}
// This code is contributed by code_hunt.
Python3
# Python3 program for the above approach
# Function to find the minimum cost
# to convert the given into
# balanced string
def findMinimumCost(s, N):
# Stores count of 1's and 0's in
# the string
count_1, count_0 = 0, 0
# Traverse the string
for i in range(N):
if (s[i] == '1'):
# Increment count1
count_1 += 1
else:
# Increment count 0
count_0 += 1
# Stores absolute difference of
# counts of 0's and 1's
k = abs(count_0 - count_1)
# If consists of only
# 0's and 1's
if (count_1 == N or count_0 == N):
print(-1)
# Print the minimum cost
else:
print(k // 2)
# Driver Code
if __name__ == '__main__':
S = "110110"
N = len(S)
findMinimumCost(S, N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum cost
// to convert the given string into
// balanced string
static void findMinimumCost(String s, int N)
{
// Stores count of 1's and 0's in
// the string
int count_1 = 0, count_0 = 0;
// Traverse the string
for(int i = 0; i < N; i++)
{
if (s[i] == '1')
// Increment count1
count_1++;
else
// Increment count 0
count_0++;
}
// Stores absolute difference of
// counts of 0's and 1's
int k = Math.Abs(count_0 - count_1);
// If string consists of only
// 0's and 1's
if (count_1 == N || count_0 == N)
Console.WriteLine(-1);
// Print minimum cost
else
Console.WriteLine(k / 2);
}
// Driver Code
static public void Main()
{
String S = "110110";
int N = S.Length;
findMinimumCost(S, N);
}
}
// This code is contributed by Dharanendra L V.
Javascript
1
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live