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 Codeint 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 approachclass 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 Codepublic 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 Codeif __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 approachusing 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 Codepublic 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 Inputlet A = 1;let B = 5;let S = "01100";// Function CallMinimumCost(S, A, B);// This code is contributed by gfgking.</script> |
6
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
