-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3579-MinimumStepsToConvertStringWithOperations.go
More file actions
150 lines (135 loc) · 4.94 KB
/
3579-MinimumStepsToConvertStringWithOperations.go
File metadata and controls
150 lines (135 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
// 3579. Minimum Steps to Convert String with Operations
// 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",
// Perform operation of type 3 on "ab" -> "ba".
// Perform operation of type 1 on "ba" -> "da".
// For the substring "c" do no operations.
// For the substring "df",
// Perform operation of type 1 on "df" -> "bf".
// Perform operation of type 1 on "bf" -> "be".
// Example 2:
// Input: word1 = "abceded", word2 = "baecfef"
// Output: 4
// Explanation:
// Divide word1 into "ab", "ce", and "ded". The operations are:
// For the substring "ab",
// Perform operation of type 2 on "ab" -> "ba".
// For the substring "ce",
// Perform operation of type 2 on "ce" -> "ec".
// For the substring "ded",
// Perform operation of type 1 on "ded" -> "fed".
// Perform operation of type 1 on "fed" -> "fef".
// Example 3:
// Input: word1 = "abcdef", word2 = "fedabc"
// Output: 2
// Explanation:
// Divide word1 into "abcdef". The operations are:
// For the substring "abcdef",
// Perform operation of type 3 on "abcdef" -> "fedcba".
// Perform operation of type 2 on "fedcba" -> "fedabc".
// Constraints:
// 1 <= word1.length == word2.length <= 100
// word1 and word2 consist only of lowercase English letters.
import "fmt"
func minOperations(word1, word2 string) int {
count := [26][26]int{}
op, n := 0, len(word1)
update := func(x, y byte) {
if x == y { return }
x -= 'a'
y -= 'a'
if count[y][x] > 0 {
count[y][x]--
} else {
count[x][y]++
op++
}
}
// 预处理 revOp
revOp := make([][]int, n)
for i := range revOp {
revOp[i] = make([]int, n)
}
// 中心扩展法
// i 为偶数表示奇长度子串,i 为奇数表示偶长度子串
for i := 0; i < 2 * n - 1; i++ {
op, count = 1, [26][26]int{}
// 从闭区间 [l,r] 开始向左右扩展
l, r := i / 2, (i+1) / 2
for l >= 0 && r < n {
update(word1[l], word2[r])
if l != r {
update(word1[r], word2[l])
}
revOp[l][r] = op
l--
r++
}
}
min := func (x, y int) int { if x < y { return x; }; return y; }
f := make([]int, n + 1)
for i := 0; i < n; i++ {
res := 1 << 31
op, count = 0, [26][26]int{} // 不反转时的最小操作次数
for j := i; j >= 0; j-- {
update(word1[j], word2[j])
res = min(res, f[j] + min(op, revOp[j][i]))
}
f[i + 1] = res
}
return f[n]
}
func main() {
// Example 1:
// Input: word1 = "abcdf", word2 = "dacbe"
// Output: 4
// Explanation:
// Divide word1 into "ab", "c", and "df". The operations are:
// For the substring "ab",
// Perform operation of type 3 on "ab" -> "ba".
// Perform operation of type 1 on "ba" -> "da".
// For the substring "c" do no operations.
// For the substring "df",
// Perform operation of type 1 on "df" -> "bf".
// Perform operation of type 1 on "bf" -> "be".
fmt.Println(minOperations("abcdf", "dacbe")) // 4
// Example 2:
// Input: word1 = "abceded", word2 = "baecfef"
// Output: 4
// Explanation:
// Divide word1 into "ab", "ce", and "ded". The operations are:
// For the substring "ab",
// Perform operation of type 2 on "ab" -> "ba".
// For the substring "ce",
// Perform operation of type 2 on "ce" -> "ec".
// For the substring "ded",
// Perform operation of type 1 on "ded" -> "fed".
// Perform operation of type 1 on "fed" -> "fef".
fmt.Println(minOperations("abceded", "baecfef")) // 4
// Example 3:
// Input: word1 = "abcdef", word2 = "fedabc"
// Output: 2
// Explanation:
// Divide word1 into "abcdef". The operations are:
// For the substring "abcdef",
// Perform operation of type 3 on "abcdef" -> "fedcba".
// Perform operation of type 2 on "fedcba" -> "fedabc".
fmt.Println(minOperations("abcdef", "fedabc")) // 2
fmt.Println(minOperations("leetcode", "bluefrog")) // 8
fmt.Println(minOperations("bluefrog", "leetcode")) // 8
}