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:
-
Replace: Replace the character at any one index of
substr
with another lowercase English letter. -
Swap: Swap any two characters in
substr
. -
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>"ab" -> "ba"</code>.</li> <li>Perform operation of type 1 on <code>"ba" -> "da"</code>.</li> </ul> </li> <li>For the substring <code>"c"</code> do no operations.</li> <li>For the substring <code>"df"</code>, <ul> <li>Perform operation of type 1 on <code>"df" -> "bf"</code>.</li> <li>Perform operation of type 1 on <code>"bf" -> "be"</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>"ab" -> "ba"</code>.</li> </ul> </li> <li>For the substring <code>"ce"</code>, <ul> <li>Perform operation of type 2 on <code>"ce" -> "ec"</code>.</li> </ul> </li> <li>For the substring <code>"ded"</code>, <ul> <li>Perform operation of type 1 on <code>"ded" -> "fed"</code>.</li> <li>Perform operation of type 1 on <code>"fed" -> "fef"</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>"abcdef" -> "fedcba"</code>.</li> <li>Perform operation of type 2 on <code>"fedcba" -> "fedabc"</code>.</li> </ul> </li>
Β
Constraints:
1 <= word1.length == word2.length <= 100
word1
andword2
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)