Watch 10 video solutions for Minimum Cost to Make All Characters Equal, a medium level problem involving String, Dynamic Programming, Greedy. This walkthrough by Prakhar Agrawal has 2,022 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a 0-indexed binary string s of length n on which you can apply two types of operations:
i and invert all characters from index 0 to index i (both inclusive), with a cost of i + 1i and invert all characters from index i to index n - 1 (both inclusive), with a cost of n - iReturn the minimum cost to make all characters of the string equal.
Invert a character means if its value is '0' it becomes '1' and vice-versa.
Example 1:
Input: s = "0011" Output: 2 Explanation: Apply the second operation withi = 2to obtains = "0000" for a cost of 2. It can be shown that 2 is the minimum cost to make all characters equal.
Example 2:
Input: s = "010101" Output: 9 Explanation: Apply the first operation with i = 2 to obtain s = "101101" for a cost of 3. Apply the first operation with i = 1 to obtain s = "011101" for a cost of 2. Apply the first operation with i = 0 to obtain s = "111101" for a cost of 1. Apply the second operation with i = 4 to obtain s = "111110" for a cost of 2. Apply the second operation with i = 5 to obtain s = "111111" for a cost of 1. The total cost to make all characters equal is 9. It can be shown that 9 is the minimum cost to make all characters equal.
Constraints:
1 <= s.length == n <= 105s[i] is either '0' or '1'Problem Overview: You are given a binary string s. In one operation you can flip either a prefix or a suffix of the string, and the cost equals the length of the flipped segment. The goal is to make all characters in the string equal with the minimum total cost.
Approach 1: Prefix and Suffix Flips (Greedy) (Time: O(n), Space: O(1))
The key observation: cost only matters when the character changes between adjacent positions. When you encounter a boundary where s[i] != s[i-1], you must perform a flip to make that region consistent with the rest. You have two options: flip the prefix ending at i-1 with cost i, or flip the suffix starting at i with cost n-i. The optimal choice is simply min(i, n-i). Iterate through the string once, detect every transition, and accumulate this minimum cost. This works because each boundary can be resolved independently, and choosing the cheaper side always leads to the minimal global cost. The algorithm uses simple iteration over the string and a greedy decision at each change point, making it both efficient and easy to implement.
Approach 2: Counting Zero Blocks and One Blocks (Greedy / DP Insight) (Time: O(n), Space: O(1))
Another way to reason about the problem is by grouping consecutive characters into blocks. Each block of 0s or 1s represents a segment that may need merging with neighboring blocks. When the character changes, you effectively decide whether to flip everything before the boundary or everything after it. Instead of thinking about individual operations, treat each boundary as a cost decision and sum the cheaper direction. This perspective highlights the relationship between block transitions and minimal flip operations. The method still runs in linear time because you scan the string once and count transitions between blocks. The logic connects closely to problems involving greedy decisions and boundary-based dynamic programming reasoning.
Recommended for interviews: The greedy boundary approach (Prefix and Suffix Flips) is the solution most interviewers expect. It shows you recognized that only transition points matter and that each can be solved independently using min(i, n-i). Explaining the block interpretation demonstrates deeper reasoning, but implementing the greedy scan proves strong algorithmic intuition and leads directly to the optimal O(n) solution.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Prefix and Suffix Flips (Greedy) | O(n) | O(1) | Best general solution. Single pass over the string with minimal memory. |
| Counting Zero and One Blocks | O(n) | O(1) | Useful for understanding the problem through block transitions and greedy cost choices. |