Minimize cost to make all characters of a Binary String equal to '1' by reversing or flipping characters of substrings
Last Updated :
08 May, 2023
Given a binary string S, and two integers A, which denotes the cost of reversing a substring, and B, which denotes the cost of flipping all characters of a substring, the task is to find the minimum cost to reduce the string S to 1s only.
Examples:
Input: S = "01100", A = 1, B = 5
Output: 6
Explanation:
One possible way to make all characters equal to '1' is as follows:
- Reverse the substring {S[0], S[2]}. Cost = A (= 1), The string modifies to "11000".
- Flip the characters of substring {S[2], S[4]}. Cost of B (= 5). The string modifies to "11111".
Therefore, the total cost = 5+1 = 6 (which is the minimum cost possible)
Input: S = "1111111", A = 2, B = 3
Output: 0
Approach: The given problem can be solved based on the following observations:
- Assuming there are P disjoint groups of continuous '0's,
- If A is less than B, then it is optimal to convert P groups into '1' group of continuous '0's by performing the first type of operation for a cost of (P - 1) * A and then converting all the '0's to '1's for a cost of B.
- Otherwise, it is optimal to perform the second operation on each group separately, for a cost of B * P.
Follow the steps below to solve the problem:
- Initialize a variable say P with 0 value to store the count of disjoint groups of continuous 0s.
- Also, initialize a variable say count as 0 to store the temporary count of the number of 0s in a group.
- Iterate over the character of the string S and do the following:
- If the current character is '0' then increment the count by 1.
- Otherwise, if the count is greater than 0 then increment P by 1 and then assign 0 to count.
- If the count is greater than 0 then increment P by 1.
- Print the minimum cost obtained as (P-1)*A+B.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate minimum cost to
// convert all the characters of S to '1'
void MinimumCost(string S, int A, int B)
{
// Stores the result
int count = 0;
// Stores the number of groups
// that have 0 as characters
int group = 0;
// Traverse the string S
for (int i = 0; i < S.size(); i++) {
// If current character is '0'
if (S[i] == '0') {
count += 1;
}
else {
// If count is greater
// than 0
if (count > 0) {
group += 1;
}
// Set the count to 0
count = 0;
}
}
// If the last few consecutive
// characters are '0'
if (count > 0)
group += 1;
// If string contains
// all characters as '1'
if (group == 0) {
cout << 0 << endl;
}
else {
// Minimum Cost
cout << min(A, B) * (group - 1) + B;
}
}
// Driver Code
int main()
{
// Given Input
int A = 1;
int B = 5;
string S = "01100";
// Function Call
MinimumCost(S, A, B);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to calculate minimum cost to
// convert all the characters of S to '1'
static void MinimumCost(String S, int A, int B)
{
// Stores the result
int count = 0;
// Stores the number of groups
// that have 0 as characters
int group = 0;
// Traverse the string S
for(int i = 0; i < S.length(); i++)
{
// If current character is '0'
if (S.charAt(i) == '0')
{
count += 1;
}
else
{
// If count is greater
// than 0
if (count > 0)
{
group += 1;
}
// Set the count to 0
count = 0;
}
}
// If the last few consecutive
// characters are '0'
if (count > 0)
group += 1;
// If string contains
// all characters as '1'
if (group == 0)
{
System.out.println(0);
}
else
{
// Minimum Cost
System.out.print(Math.min(A, B) *
(group - 1) + B);
}
}
// Driver Code
public static void main(String args[])
{
// Given Input
int A = 1;
int B = 5;
String S = "01100";
// Function Call
MinimumCost(S, A, B);
}
}
// This code is contributed by SoumikMondal
Python3
# Python3 program for the above approach
# Function to calculate minimum cost to
# convert all the characters of S to '1'
def MinimumCost(S, A, B):
# Stores the result
count = 0
# Stores the number of groups
# that have 0 as characters
group = 0
# Traverse the S
for i in range(len(S)):
# If current character is '0'
if (S[i] == '0'):
count += 1
else:
# If count is greater
# than 0
if (count > 0):
group += 1
# Set the count to 0
count = 0
# If the last few consecutive
# characters are '0'
if (count > 0):
group += 1
# If contains
# all characters as '1'
if (group == 0):
print(0)
else:
# Minimum Cost
print(min(A, B) * (group - 1) + B)
# Driver Code
if __name__ == '__main__':
# Given Input
A = 1
B = 5
S = "01100"
# Function Call
MinimumCost(S, A, B)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate minimum cost to
// convert all the characters of S to '1'
static void MinimumCost(string S, int A, int B)
{
// Stores the result
int count = 0;
// Stores the number of groups
// that have 0 as characters
int group = 0;
// Traverse the string S
for(int i = 0; i < S.Length; i++)
{
// If current character is '0'
if (S[i] == '0')
{
count += 1;
}
else
{
// If count is greater
// than 0
if (count > 0)
{
group += 1;
}
// Set the count to 0
count = 0;
}
}
// If the last few consecutive
// characters are '0'
if (count > 0)
group += 1;
// If string contains
// all characters as '1'
if (group == 0)
{
Console.WriteLine(0);
}
else
{
// Minimum Cost
Console.Write(Math.Min(A, B) *
(group - 1) + B);
}
}
// Driver Code
public static void Main()
{
// Given Input
int A = 1;
int B = 5;
string S = "01100";
// Function Call
MinimumCost(S, A, B);
}
}
// This code is contributed by SURENDRA_GANGWAR
JavaScript
<script>
// Javascript program for the above approach
// Function to calculate minimum cost to
// convert all the characters of S to '1'
function MinimumCost(S, A, B) {
// Stores the result
let count = 0;
// Stores the number of groups
// that have 0 as characters
let group = 0;
// Traverse the string S
for (let i = 0; i < S.length; i++) {
// If current character is '0'
if (S[i] == '0') {
count += 1;
}
else
{
// If count is greater
// than 0
if (count > 0) {
group += 1;
}
// Set the count to 0
count = 0;
}
}
// If the last few consecutive
// characters are '0'
if (count > 0)
group += 1;
// If string contains
// all characters as '1'
if (group == 0) {
document.write(0 + "<br>");
}
else {
// Minimum Cost
document.write(Math.min(A, B) * (group - 1) + B);
}
}
// Driver Code
// Given Input
let A = 1;
let B = 5;
let S = "01100";
// Function Call
MinimumCost(S, A, B);
// This code is contributed by gfgking.
</script>
Time Complexity: O(N), The time complexity is O(n), where n is the length of the input string S. This is because the program traverses the entire string once to count the number of groups of consecutive 0's, and then performs a constant number of operations to compute and output the minimum cost. Therefore, the time complexity is linear in the length of the input string.
Auxiliary Space: O(1), The space complexity of the program is O(1), which is constant. This is because the program only uses a fixed number of integer variables to store the count and group information, and a constant amount of space is used for the input string and any other internal data structures created by the program. Thus, the space used by the program does not depend on the size of the input, and is constant irrespective of the length of the input string.
Similar Reads
Minimize flips to make binary string as all 1s by flipping characters in substring of size K repeatedly Given a binary string S of size N and an integer K, the task is to find the minimum number of operations required to make all characters as 1s in the binary string by flipping characters in the substring of size K. If it is not possible to do so, then print "-1". Examples: Input: S = "00010110 ", K
7 min read
Maximum length of a substring required to be flipped repeatedly to make all characters of binary string equal to 0 Given a binary string S, the task is to find the maximum length of substrings required to be flipped repeatedly to make all the characters of a binary string equal to '0'. Examples: Input: S = "010"Output: 2Explanation:Following are the order of flipping of substring of at least K for the value of K
6 min read
Minimum flips or swapping of adjacent characters required to make a string equal to another Given two binary strings A and B of length N, the task is to convert the string A to B by either flipping any character of A or swapping adjacent characters of A minimum number of times. If it is not possible to make both the strings equal, print -1. Examples: Input: A = "10010010", B = "00001000" O
6 min read
Min flips of continuous characters to make all characters same in a string Given a string consisting only of 1's and 0's. In one flip we can change any continuous sequence of this string. Find this minimum number of flips so the string consist of same characters only.Examples: Input : 00011110001110Output : 2We need to convert 1's sequenceso string consist of all 0's.Input
8 min read
Minimum cost of flipping characters required to convert Binary String to 0s only Given binary string str, and integers A, which denotes the cost of converting consecutive 1s to 0s, and B, which denotes the cost of converting 0s to 1s. The task is to find the minimum cost to reduce the string str to 0s only. Examples: Input: str = â01101110â, A = 5, B = 1Output: 6Explanation:Conv
8 min read
Minimize flipping of bits in given Binary string to make count of 10 equal to 01 Given binary string str, the task is to choose any index and change into 0 or 1, and do this in minimum steps such that the count of substring 01 is equal to 10. Examples: Input: str = "01101"Output: 01100Explanation: 01 as a substring repeat 2 times in a string, 10 as a substring repeat 1 times in
5 min read