Apply Operations to Make Two Strings Equal - Problem

You're given two binary strings s1 and s2 of equal length n, and a positive integer x. Your goal is to transform s1 into s2 using the minimum cost operations.

Available Operations:

  • Operation 1: Choose any two indices i and j, flip both s1[i] and s1[j] (cost: x)
  • Operation 2: Choose an index i where i < n-1, flip both s1[i] and s1[i+1] (cost: 1)

Flipping a character means changing '0' to '1' or '1' to '0'. Return the minimum cost to make the strings equal, or -1 if impossible.

Example: If s1 = "1100" and s2 = "0011", you need to fix positions 0, 1, 2, and 3. You could use adjacent flips (cost 1 each) or strategic any-two-indices flips (cost x each).

Input & Output

example_1.py — Basic Case
$ Input: s1 = "1100", s2 = "0011", x = 3
Output: 4
💡 Note: Differences at positions [0,1,2,3]. We can pair (0,1) with adjacent operation (cost 1) and (2,3) with adjacent operation (cost 1), but positions 1 and 2 are also adjacent. Optimal: pair (0,3) with any-pair operation (cost 3) and (1,2) with adjacent operation (cost 1), total = 4.
example_2.py — Impossible Case
$ Input: s1 = "100", s2 = "001", x = 2
Output: -1
💡 Note: Differences at positions [0,2]. Since we have odd number of differences (1 difference at positions 0 and 2 = 2 total, but we need pairs), it's impossible to make them equal using operations that flip exactly 2 positions each.
example_3.py — All Adjacent
$ Input: s1 = "0000", s2 = "1111", x = 10
Output: 2
💡 Note: Differences at all positions [0,1,2,3]. We can pair adjacent positions: (0,1) with cost 1 and (2,3) with cost 1. Total cost = 2, which is much better than using any-pair operations.

Constraints

  • 1 ≤ n ≤ 1000 where n is the length of both strings
  • 1 ≤ x ≤ 106
  • s1 and s2 consist of only characters '0' and '1'
  • Both strings have equal length

Visualization

Tap to expand
Apply Operations to Make Two Strings Equal INPUT s1 = "1100" 1 1 0 0 i=0 i=1 i=2 i=3 s2 = "0011" 0 0 1 1 Differences at indices: 0 1 2 3 x = 3 (Op1 cost) n = 4 (length) diff_count = 4 (even) ALGORITHM STEPS 1 Find Differences diff = [0, 1, 2, 3] 2 Use DP Strategy Pair adjacent diffs 3 Compare Options Op2: gap cost = d[i+1]-d[i] Op1: any pair cost = x 4 Calculate Minimum Choose optimal pairing Cost Calculation: Pair (0,1): gap=1, cost=1 Pair (2,3): gap=1, cost=1 Total Op2: 1+1+1+1 = 4 Op1 (x=3) not optimal min(4, 3+...) = 4 FINAL RESULT Optimal Operations: Start: 1 1 0 0 Op2 at i=0: flip [0,1] --> 0 0 0 0 (cost: 1) Op2 at i=1: flip [1,2] --> 0 1 1 0 (cost: 1) Op2 at i=1: flip [1,2] --> 0 0 0 0 (cost: 1) Op2 at i=2: flip [2,3] --> 0 0 1 1 (cost: 1) Output: 4 OK - s1 equals s2 Key Insight: 1. If difference count is odd, transformation is impossible (return -1). 2. Use DP to pair differences optimally: adjacent diff pairs use Op2 (cost = gap), or any two positions use Op1 (cost = x). Compare min(adjacent_cost, x) for each pair. TutorialsPoint - Apply Operations to Make Two Strings Equal | Optimal Solution
Asked in
Google 28 Meta 22 Amazon 18 Microsoft 15
28.5K Views
Medium Frequency
~25 min Avg. Time
890 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