LeetCode 2976: Minimum Cost to Convert String I Solution

Master LeetCode problem 2976 (Minimum Cost to Convert String I), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2976. Minimum Cost to Convert String I

Problem Explanation

Explanation

To solve this problem, we can use a dynamic programming approach. We will create a 2D dp array where dp[i][j] represents the minimum cost to convert the string source[0...i] to target[0...j]. We will iterate through each character in the source and target strings and update the minimum cost based on whether we change the character or not.

At each step, we have two choices:

  1. Change the character at index i in source to match the character at index j in target.
  2. Keep the character at index i in source unchanged.

We will choose the option with the minimum cost. If both characters are already the same, the cost will be 0.

If it is not possible to convert source to target, we will return -1.

Time Complexity: O(n^2), where n is the length of the strings source and target. Space Complexity: O(n^2), to store the dp array.

Solution Code

class Solution {
    public int minimumCost(String source, String target, char[] original, char[] changed, int[] cost) {
        int n = source.length();
        int m = target.length();

        int[][] dp = new int[n + 1][m + 1];

        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                if (i == 0 || j == 0) {
                    dp[i][j] = 0;
                } else {
                    dp[i][j] = Math.min(dp[i - 1][j] + (i > 0 ? cost[source.charAt(i - 1) - 'a'] : 0),
                                        dp[i][j - 1] + (j > 0 ? cost[target.charAt(j - 1) - 'a'] : 0));

                    if (i > 0 && j > 0 && source.charAt(i - 1) == target.charAt(j - 1)) {
                        dp[i][j] = Math.min(dp[i][j], dp[i - 1][j - 1]);
                    }

                    for (int k = 0; k < original.length; k++) {
                        if (i > 0 && j > 0 && source.charAt(i - 1) == original[k] && target.charAt(j - 1) == changed[k]) {
                            dp[i][j] = Math.min(dp[i][j], dp[i - 1][j - 1] + cost[k]);
                        }
                    }
                }
            }
        }

        return dp[n][m] == Integer.MAX_VALUE ? -1 : dp[n][m];
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2976 (Minimum Cost to Convert String I)?

This page provides optimized solutions for LeetCode problem 2976 (Minimum Cost to Convert String I) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2976 (Minimum Cost to Convert String I)?

The time complexity for LeetCode 2976 (Minimum Cost to Convert String I) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2976 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2976 in Java, C++, or Python.

Back to LeetCode Solutions