↓Skip to main content
  1. LeetCode/

LeetCode 3800: Minimum Cost to Make Two Binary Strings Equal

·2 mins· ·
LeetCode Medium Greedy String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-04

  • Submissions: Accepted (first try)

Approach
#

Only positions with s[i] != t[i] matter, in two kinds:

  • c0: s[i] = 0, t[i] = 1.
  • c1: s[i] = 1, t[i] = 0.

Fixes (Greedy Template 4, pairing and parity):

CaseHowCost
One c0 with one c1swap the two cells within smin(swap, 2 × flip)
Two of the same kindcross one so it becomes the other kind, then swapmin(cross + swap, 2 × flip)
One left overflipflip

Use as many different-kind pairs as possible (min(c0, c1)); the remaining |c0 - c1| same-kind cells pair up, and an odd one is flipped.

My Solution
#

I originally listed three whole plans and took the minimum: flip everything; swap different kinds and flip the rest; swap different kinds, cross + swap same kinds, flip the leftover. Here it is reorganized as the cheapest option per pair type:

class Solution:
    def minimumCost(self, s: str, t: str, flipCost: int, swapCost: int, crossCost: int) -> int:
        c = [0, 0]
        for a, b in zip(s, t):
            if a != b:
                c[int(a)] += 1
        pairs = min(c)                 # different-kind pairs
        rest = abs(c[0] - c[1])        # the rest are all one kind
        return (pairs * min(swapCost, 2 * flipCost)
                + rest // 2 * min(crossCost + swapCost, 2 * flipCost)
                + rest % 2 * flipCost)

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Two same-kind cells cannot simply be swapped (swapping equal values does nothing); cross one first.
  • Compare every pair type against two flips; flipping may be cheaper.

Interview Explanation
#

I split mismatches by whether s holds 0 or 1. A pair of different kinds is fixed by one swap, a same-kind pair by crossing one then swapping, and a leftover by a flip; each option is compared with two flips. Time O(n).

Related

LeetCode 2131: Longest Palindrome by Concatenating Two Letter Words
·2 mins
LeetCode Medium Greedy Hash-Table String Counting
LeetCode 2131 note: pair each word with its reverse on both sides; self-palindromes also pair with each other, and only one leftover can sit in the middle
LeetCode 402: Remove K Digits
·2 mins
LeetCode Medium String Stack Greedy Monotonic-Stack
LeetCode 402 note: a monotonically increasing stack; while deletions remain, pop any top larger than the current digit, then strip leading zeros
LeetCode 3799: Word Squares II
·2 mins
LeetCode Medium Backtracking Enumeration String
LeetCode 3799 note: four nested levels pruned by corner letters; sorting the words first makes the output lexicographic
LeetCode 1016: Binary String With Substrings Representing 1 To N
·3 mins
LeetCode Medium Sliding-Window String Bit-Manipulation Hash-Table
LeetCode 1016 note: s has at most 1000 characters, so there are few substrings of any fixed length and a large n must be False; checking from n downward fails fast, or extend up to 30 bits from each start to collect every value <= n
LeetCode 1888: Minimum Number of Flips to Make the Binary String Alternating
·2 mins
LeetCode Medium Sliding-Window String Dynamic-Programming
LeetCode 1888 note: rotations become length-n windows over s + s; the window’s mismatches against 0101… are diff, and against 1010… they are n - diff
LeetCode 3796: Find Maximum Value in a Constrained Sequence
·2 mins
LeetCode Medium Greedy Array
LeetCode 3796 note: each position’s bound is the min of its own limit, the left bound + difference and the right bound + difference; one pass each way