Tap here to switch tabs
Problem
Submissions

Problem: Apply Operations to Make Two Strings Equal

med
30 min
Try to solve the Apply Operations to Make Two Strings Equal problem.

Statement

You are given two binary strings s1 and s2 of length n, and an integer x.

You can apply any of the following two operations on s1 any number of times:

  • Select two distinct indices i and j such that 0≤i<j<n0 \leq i < j < n, and invert the characters at s1[i] and s1[j]. This operation costs x.

  • Select an index i such that 0≤i<n−10 \leq i < n - 1, and invert the characters at both s1[i] and s1[i + 1]. This operation costs 11.

Return the minimum total cost needed to make s1 identical to s2, or −1-1 if it is impossible to make them equal.

Note: Inverting a character changes ′0′'0' to ′1′'1' and ′1′'1' to ′0′'0'.

Constraints:

  • n == s1.length == s2.length

  • 1≤1 \leq n, x ≤500\leq 500

  • s1 and s2 consist only of the characters ′0′'0' and ′1′'1'.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Apply Operations to Make Two Strings Equal

med
30 min
Try to solve the Apply Operations to Make Two Strings Equal problem.

Statement

You are given two binary strings s1 and s2 of length n, and an integer x.

You can apply any of the following two operations on s1 any number of times:

  • Select two distinct indices i and j such that 0≤i<j<n0 \leq i < j < n, and invert the characters at s1[i] and s1[j]. This operation costs x.

  • Select an index i such that 0≤i<n−10 \leq i < n - 1, and invert the characters at both s1[i] and s1[i + 1]. This operation costs 11.

Return the minimum total cost needed to make s1 identical to s2, or −1-1 if it is impossible to make them equal.

Note: Inverting a character changes ′0′'0' to ′1′'1' and ′1′'1' to ′0′'0'.

Constraints:

  • n == s1.length == s2.length

  • 1≤1 \leq n, x ≤500\leq 500

  • s1 and s2 consist only of the characters ′0′'0' and ′1′'1'.