Problem Link

Description


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 substrings. 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:

  • For the substring "ab",
    <ul>
    	<li>Perform operation of type 3 on <code>&quot;ab&quot; -&gt; &quot;ba&quot;</code>.</li>
    	<li>Perform operation of type 1 on <code>&quot;ba&quot; -&gt; &quot;da&quot;</code>.</li>
    </ul>
    </li>
    <li>For the substring <code>&quot;c&quot;</code> do no operations.</li>
    <li>For the substring <code>&quot;df&quot;</code>,
    <ul>
    	<li>Perform operation of type 1 on <code>&quot;df&quot; -&gt; &quot;bf&quot;</code>.</li>
    	<li>Perform operation of type 1 on <code>&quot;bf&quot; -&gt; &quot;be&quot;</code>.</li>
    </ul>
    </li>
    

Example 2:

Input: word1 = "abceded", word2 = "baecfef"

Output: 4

Explanation:

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

  • For the substring "ab",
    <ul>
    	<li>Perform operation of type 2 on <code>&quot;ab&quot; -&gt; &quot;ba&quot;</code>.</li>
    </ul>
    </li>
    <li>For the substring <code>&quot;ce&quot;</code>,
    <ul>
    	<li>Perform operation of type 2 on <code>&quot;ce&quot; -&gt; &quot;ec&quot;</code>.</li>
    </ul>
    </li>
    <li>For the substring <code>&quot;ded&quot;</code>,
    <ul>
    	<li>Perform operation of type 1 on <code>&quot;ded&quot; -&gt; &quot;fed&quot;</code>.</li>
    	<li>Perform operation of type 1 on <code>&quot;fed&quot; -&gt; &quot;fef&quot;</code>.</li>
    </ul>
    </li>
    

Example 3:

Input: word1 = "abcdef", word2 = "fedabc"

Output: 2

Explanation:

Divide word1 into "abcdef". The operations are:

  • For the substring "abcdef",
    <ul>
    	<li>Perform operation of type 3 on <code>&quot;abcdef&quot; -&gt; &quot;fedcba&quot;</code>.</li>
    	<li>Perform operation of type 2 on <code>&quot;fedcba&quot; -&gt; &quot;fedabc&quot;</code>.</li>
    </ul>
    </li>
    

Β 

Constraints:

  • 1 <= word1.length == word2.length <= 100
  • word1 and word2 consist only of lowercase English letters.

Solution


Python3

class Solution:
    def minOperations(self, word1: str, word2: str) -> int:
        N = len(word1)
 
        def solve(i, j, isReversed):
            operations = 1 if isReversed else 0
            count = [[0] * 26 for _ in range(26)]
            index1 = i
            index2 = j if isReversed else i
            add = -1 if isReversed else 1
 
            for _ in range(j - i + 1):
                if word1[index1] != word2[index2]:
                    got, wanted = ord(word1[index1]) - ord('a'), ord(word2[index2]) - ord('a')
                    if count[got][wanted] > 0:
                        count[got][wanted] -= 1
                    else:
                        count[wanted][got] += 1
                        operations += 1
                
                index1 += 1
                index2 += add
 
            return operations
 
        @cache
        def go(i, j):
            if i >= N: return 0
            if j >= N: return inf
 
            skip = go(i, j + 1)
            take = min(solve(i, j, False), solve(i, j, True)) + go(j + 1, j + 1)
            return min(skip, take)
        
        return go(0, 0)