LeetCode in Kotlin

2896. Apply Operations to Make Two Strings Equal

Medium

You are given two 0-indexed binary strings s1 and s2, both of length n, and a positive integer x.

You can perform any of the following operations on the string s1 any number of times:

Return the minimum cost needed to make the strings s1 and s2 equal, or return -1 if it is impossible.

Note that flipping a character means changing it from 0 to 1 or vice-versa.

Example 1:

Input: s1 = “1100011000”, s2 = “0101001010”, x = 2

Output: 4

Explanation: We can do the following operations:

The total cost is 1 + 1 + 2 = 4. It can be shown that it is the minimum cost possible.

Example 2:

Input: s1 = “10110”, s2 = “00011”, x = 4

Output: -1

Explanation: It is not possible to make the two strings equal.

Constraints:

Solution

import kotlin.math.min

class Solution {
    fun minOperations(s1: String, s2: String, x: Int): Int {
        val n = s1.length
        val diffs = ArrayList<Int>()
        for (i in 0 until n) {
            if (s1[i] != s2[i]) {
                diffs.add(i)
            }
        }
        val m = diffs.size
        if ((m and 1) == 1) {
            return -1
        } else if (m == 0) {
            return 0
        }
        val dp = IntArray(m)
        dp[0] = 0
        dp[1] = min(x.toDouble(), (diffs[1] - diffs[0]).toDouble()).toInt()
        for (i in 2 until m) {
            if ((i and 1) == 1) {
                dp[i] = min((dp[i - 1] + x).toDouble(), (dp[i - 2] + diffs[i] - diffs[i - 1]).toDouble())
                    .toInt()
            } else {
                dp[i] = min(dp[i - 1].toDouble(), (dp[i - 2] + diffs[i] - diffs[i - 1]).toDouble())
                    .toInt()
            }
        }
        return dp[m - 1]
    }
}