Minimize the cost to convert given string to either type XYXY… or XXYY…


In this problem, we need to convert the given binary string to abababab or aabbaabb format and find the minimum cost for that. Also, we have given the cost to flip any character in the operations array.

Problem statement - We have given a bin_str binary string and operations array containing the positive integers. The size of the string and array is the same and even. The task is to find the minimum costs to convert the string in the ababab… or aabbaabb… format. The cost to flip any character in the given string is the value at the ith index in the operations array.

Sample examples

Input

bin_Str = "1001", operations[] = {1, 3, 2, 3}

Output

3

Explanation

  • To convert the string into 1010, the cost 2 + 3 = 5 as we need to flip the last two bits.

  • To convert the string to 0101, the cost is 1 + 3 = 4, as we need to flip the first two bits.

  • For 1100 strings, the cost is 3 + 3 = 6.

  • For 0011, the cost is 1 + 2 = 3

So, the minimum cost is 3.

Input

bin_Str = "11001100", operations[] = {1, 3, 2, 3, 2, 3, 4, 2}

Output

0

Explanation - The string is already in the required format.

Input

bin_Str = "01010101010101", operations[] = {1, 3, 2, 3, 2, 3, 4, 2}

Output

0

Explanation - The string is already in the required format.

Approach 1

We can observe that there are only 4 possible ways to create binary strings according to the given conditions.

  • 01010101010101….

  • 1010101010101010…..

  • 001100110011001100…

  • 1100110011001100…

We can try to convert strings in all possible combinations and calculate the cost. After that, we can take the minimum cost as an answer to the problem.

Algorithm

Step 1 - If the string length is 2, return 0, as it will always be in the given format.

Step 2 - Execute the helper() function by passing the string format as a parameter.

Step 3 - In the helper() function, Initialize the 'cnt' variable with 0 to store the cost.

Step 4 - Start traversing the string. If the character at the pth index is not equal to 'a', add operations[p] into the 'cnt' as we need to flip the character.

Step 5 - If the character at p + 1, p + 2, and p + 3 index is not the same as the b, c, and d characters, add their cost to the 'cnt'.

Step 6 - Return the 'cnt' value from the helper() function.

Step 7 - In the totalOperations() function, get the cost to convert a string in every four formats.

Step 8 - After that, return the minimum value from x, y, z, and w.

Example

#include <bits/stdc++.h>
using namespace std;

int helper(string bin_Str, int operations[], char a, char b, char c, char 
d) {
   int cnt = 0;
   // Traverse the string
   for (int p = 0; p < bin_Str.length(); p += 4) {
      // Count the cost to convert the string to the required form
      if (bin_Str[p] != a)
         cnt += operations[p];
      if (p + 1 < bin_Str.length() && bin_Str[p + 1] != b)
         cnt += operations[p + 1];
      if (p + 2 < bin_Str.length() && bin_Str[p + 2] != c)
         cnt += operations[p + 2];
      if (p + 3 < bin_Str.length() && bin_Str[p + 3] != d)
         cnt += operations[p + 3];
   }
   return cnt;
}
int totalOperations(int op_len, string bin_Str, int operations[]) {
   // For the string of length 2
   if (bin_Str.length() == 2) {
      return 0;
   }
   // For the strings of length 4 or more
   int x = helper(bin_Str, operations, '0', '1', '0', '1');
   int y = helper(bin_Str, operations, '1', '0', '1', '0');
   int z = helper(bin_Str, operations, '1', '1', '0', '0');
   int w = helper(bin_Str, operations, '0', '0', '1', '1');
   return min({x, y, z, w});
}
int main() {
   int op_len = 4;
   string bin_Str = "1001";
   int operations[] = {1, 3, 2, 3};
   cout << "The minimum operations required to update the binary string is " << totalOperations(op_len, bin_Str, operations) << "\n";
   return 0;
}

Output

The minimum operations required to update the binary string is 3

Time complexity - O(N) to traverse the string in the helper() function.

Space complexity - O(1) as we use the constant space.

In the above solution, we converted the binary string into the given formats by flipping its character and calculated the minimum cost according to the integers given in the array. Programmers can calculate the maximum cost to convert the string into the given formats.

Updated on: 24-Aug-2023

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements