LeetCode-in-Java

3579. Minimum Steps to Convert String with Operations

Hard

You are given two strings, word1 and word2, of equal length. You need to transform word1 into word2.

For this, divide word1 into one or more contiguous **substring**. For each substring substr you can perform the following operations:

  1. Replace: Replace the character at any one index of substr with another lowercase English letter.

  2. Swap: Swap any two characters in substr.

  3. Reverse Substring: Reverse substr.

Each of these counts as one operation and each character of each substring can be used in each type of operation at most once (i.e. no single index may be involved in more than one replace, one swap, or one reverse).

Return the minimum number of operations required to transform word1 into word2.

Example 1:

Input: word1 = “abcdf”, word2 = “dacbe”

Output: 4

Explanation:

Divide word1 into "ab", "c", and "df". The operations are:

Example 2:

Input: word1 = “abceded”, word2 = “baecfef”

Output: 4

Explanation:

Divide word1 into "ab", "ce", and "ded". The operations are:

Example 3:

Input: word1 = “abcdef”, word2 = “fedabc”

Output: 2

Explanation:

Divide word1 into "abcdef". The operations are:

Constraints:

Solution

public class Solution {
    public int minOperations(String word1, String word2) {
        int[] dp = new int[word1.length()];
        int[][] count = new int[26][26];
        for (int i = 0; i < word1.length(); i++) {
            dp[i] = Integer.MAX_VALUE;
        }
        for (int i = 0; i < word1.length(); i++) {
            for (int j = i; j >= 0; j--) {
                int c1 = 0;
                int c2 = 0;
                for (int k1 = j, k2 = j; k1 <= i && k2 <= i; k1++, k2++) {
                    int[] ints = count[word2.charAt(k2) - 'a'];
                    if (ints[word1.charAt(k1) - 'a'] > 0) {
                        ints[word1.charAt(k1) - 'a']--;
                    } else if (word1.charAt(k1) != word2.charAt(k2)) {
                        count[word1.charAt(k1) - 'a'][word2.charAt(k2) - 'a']++;
                        c1++;
                    }
                }
                for (int k1 = j, k2 = j; k1 <= i && k2 <= i; k1++, k2++) {
                    count[word1.charAt(k1) - 'a'][word2.charAt(k2) - 'a'] = 0;
                }
                dp[i] = Math.min(dp[i], j - 1 < 0 ? c1 : dp[j - 1] + c1);
                for (int k1 = j, k2 = i; k1 <= i && k2 >= j; k1++, k2--) {
                    int[] ints = count[word2.charAt(k2) - 'a'];
                    if (ints[word1.charAt(k1) - 'a'] > 0) {
                        ints[word1.charAt(k1) - 'a']--;
                    } else if (word1.charAt(k1) - 'a' != word2.charAt(k2) - 'a') {
                        count[word1.charAt(k1) - 'a'][word2.charAt(k2) - 'a']++;
                        c2++;
                    }
                }
                for (int k1 = j, k2 = i; k1 <= i && k2 >= j; k1++, k2--) {
                    count[word1.charAt(k1) - 'a'][word2.charAt(k2) - 'a'] = 0;
                }
                dp[i] = Math.min(dp[i], j - 1 < 0 ? c2 + 1 : dp[j - 1] + c2 + 1);
            }
        }
        return dp[word1.length() - 1];
    }
}