Minimum Cost to Make All Characters Equal - Problem

You're given a binary string consisting of only '0's and '1's, and you need to make all characters in the string equal with the minimum possible cost.

You have two powerful operations at your disposal:

  • Left Operation: Choose any index i and flip all characters from the beginning (index 0) up to index i (inclusive). This costs i + 1
  • Right Operation: Choose any index i and flip all characters from index i to the end of the string. This costs n - i

When you "flip" a character, '0' becomes '1' and '1' becomes '0'.

Goal: Find the minimum cost to make all characters in the string identical (either all '0's or all '1's).

Example: For string "0011", you could flip from index 2 to end (cost 2) to get "0000", or use other combinations of operations.

Input & Output

example_1.py โ€” Basic case
$ Input: s = "0011"
โ€บ Output: 2
๐Ÿ’ก Note: We have one transition at position 1 (between s[1]='0' and s[2]='1'). We can choose left operation (cost 2) or right operation (cost 2). Both cost the same, so minimum cost is 2.
example_2.py โ€” Multiple transitions
$ Input: s = "010101"
โ€บ Output: 9
๐Ÿ’ก Note: Transitions at positions 0,1,2,3,4. Costs: min(1,5)=1, min(2,4)=2, min(3,3)=3, min(4,2)=2, min(5,1)=1. Total: 1+2+3+2+1=9.
example_3.py โ€” Already uniform
$ Input: s = "0000"
โ€บ Output: 0
๐Ÿ’ก Note: All characters are already the same, so no operations are needed. Cost is 0.

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s[i] is either '0' or '1'
  • Binary string only - no other characters allowed

Visualization

Tap to expand
๐Ÿญ Light Switch Factory ProblemInitial State: Mixed ON/OFF switchesOFFOFFONONOFFProblem!Problem!๐Ÿ”ง Tool Options for Each Problem:Problem at position 1-2๐Ÿ”จ Left Lever cost: 2 (flip positions 0,1)๐Ÿ”ง Right Lever cost: 3 (flip positions 2,3,4)โœ… Choose Left: cheaper (cost 2)Problem at position 3-4๐Ÿ”จ Left Lever cost: 4 (flip positions 0,1,2,3)๐Ÿ”ง Right Lever cost: 1 (flip position 4)โœ… Choose Right: cheaper (cost 1)๐ŸŽฏ Final Solution:Total Cost = 2 + 1 = 3Only fix the boundaries, not every individual switch!๐Ÿ’ก Key Insight: You don't need to flip every switch individually.Just identify where the pattern changes and use the most cost-effective tool!
Understanding the Visualization
1
Identify Problem Areas
Find where adjacent switches are in different positions - these are the 'problem boundaries'
2
Calculate Tool Costs
For each boundary, calculate the cost of using Left Lever vs Right Lever
3
Choose Cheaper Tool
Pick the less expensive tool for each boundary and sum up all costs
Key Takeaway
๐ŸŽฏ Key Insight: Focus on transitions (boundaries) between different characters, not individual characters. For each transition, greedily choose the cheaper operation.
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28
18.5K Views
Medium Frequency
~18 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen