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.

Visualization

Tap to expand
String Transformation Processs1:1100s2:0011Differences at positions: [0, 1, 2, 3]Available Operations:๐Ÿ”ง Operation 1: Flip any two positions (Cost: x)โšก Operation 2: Flip adjacent positions (Cost: 1)Goal: Find minimum cost combinationOptimal Solution:Pair (0,3)Cost: xPair (1,2)Cost: 1Why This is Optimal:โ€ข Positions 1,2 are adjacent โ†’ use cheap operationโ€ข Positions 0,3 need expensive operationโ€ข Total cost: x + 1 (better than other pairings)DP explores all pairings to find this optimal solution
Understanding the Visualization
1
Identify Mismatches
Find all positions where s1 and s2 differ - these are our 'broken switches'
2
Check Feasibility
If odd number of differences, impossible since each operation fixes exactly 2 positions
3
Optimal Pairing
Use DP to find minimum cost pairing: adjacent fixes cost 1, any-pair fixes cost x
4
Apply Operations
Execute the optimal sequence of operations to transform s1 into s2
Key Takeaway
๐ŸŽฏ Key Insight: The problem reduces to optimally pairing difference positions. Use dynamic programming with memoization to explore all pairing possibilities while avoiding redundant calculations. Always prefer adjacent operations (cost 1) over any-pair operations (cost x) when possible.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n^2 * 2^k)

Where k is number of differences. With memoization, many subproblems are avoided

n
2n
โš  Quadratic Growth
Space Complexity
O(2^k)

Space for memoization table storing results for different subsequences

n
2n
โœ“ Linear Space

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
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