Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc357fd

Browse files
feat: add solutions to lc problem: No.3579 (#4481)
No.3579.Minimum Steps to Convert String with Operations
1 parent 2f33e2a commit dc357fd

File tree

7 files changed

+560
-6
lines changed

7 files changed

+560
-6
lines changed

‎solution/3500-3599/3579.Minimum Steps to Convert String with Operations/README.md‎

Lines changed: 188 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,210 @@ tags:
151151
#### Python3
152152

153153
```python
154-
154+
class Solution:
155+
def minOperations(self, word1: str, word2: str) -> int:
156+
def calc(l: int, r: int, rev: bool) -> int:
157+
cnt = Counter()
158+
res = 0
159+
for i in range(l, r + 1):
160+
j = r - (i - l) if rev else i
161+
a, b = word1[j], word2[i]
162+
if a != b:
163+
if cnt[(b, a)] > 0:
164+
cnt[(b, a)] -= 1
165+
else:
166+
cnt[(a, b)] += 1
167+
res += 1
168+
return res
169+
170+
n = len(word1)
171+
f = [inf] * (n + 1)
172+
f[0] = 0
173+
for i in range(1, n + 1):
174+
for j in range(i):
175+
t = min(calc(j, i - 1, False), 1 + calc(j, i - 1, True))
176+
f[i] = min(f[i], f[j] + t)
177+
return f[n]
155178
```
156179

157180
#### Java
158181

159182
```java
160-
183+
class Solution {
184+
public int minOperations(String word1, String word2) {
185+
int n = word1.length();
186+
int[] f = new int[n + 1];
187+
Arrays.fill(f, Integer.MAX_VALUE);
188+
f[0] = 0;
189+
for (int i = 1; i <= n; i++) {
190+
for (int j = 0; j < i; j++) {
191+
int a = calc(word1, word2, j, i - 1, false);
192+
int b = 1 + calc(word1, word2, j, i - 1, true);
193+
int t = Math.min(a, b);
194+
f[i] = Math.min(f[i], f[j] + t);
195+
}
196+
}
197+
return f[n];
198+
}
199+
200+
private int calc(String word1, String word2, int l, int r, boolean rev) {
201+
int[][] cnt = new int[26][26];
202+
int res = 0;
203+
for (int i = l; i <= r; i++) {
204+
int j = rev ? r - (i - l) : i;
205+
int a = word1.charAt(j) - 'a';
206+
int b = word2.charAt(i) - 'a';
207+
if (a != b) {
208+
if (cnt[b][a] > 0) {
209+
cnt[b][a]--;
210+
} else {
211+
cnt[a][b]++;
212+
res++;
213+
}
214+
}
215+
}
216+
return res;
217+
}
218+
}
161219
```
162220

163221
#### C++
164222

165223
```cpp
166-
224+
class Solution {
225+
public:
226+
int minOperations(string word1, string word2) {
227+
int n = word1.length();
228+
vector<int> f(n + 1, INT_MAX);
229+
f[0] = 0;
230+
231+
for (int i = 1; i <= n; ++i) {
232+
for (int j = 0; j < i; ++j) {
233+
int a = calc(word1, word2, j, i - 1, false);
234+
int b = 1 + calc(word1, word2, j, i - 1, true);
235+
int t = min(a, b);
236+
f[i] = min(f[i], f[j] + t);
237+
}
238+
}
239+
240+
return f[n];
241+
}
242+
243+
private:
244+
int calc(const string& word1, const string& word2, int l, int r, bool rev) {
245+
int cnt[26][26] = {0};
246+
int res = 0;
247+
248+
for (int i = l; i <= r; ++i) {
249+
int j = rev ? r - (i - l) : i;
250+
int a = word1[j] - 'a';
251+
int b = word2[i] - 'a';
252+
253+
if (a != b) {
254+
if (cnt[b][a] > 0) {
255+
cnt[b][a]--;
256+
} else {
257+
cnt[a][b]++;
258+
res++;
259+
}
260+
}
261+
}
262+
263+
return res;
264+
}
265+
};
167266
```
168267
169268
#### Go
170269
171270
```go
271+
func minOperations(word1 string, word2 string) int {
272+
n := len(word1)
273+
f := make([]int, n+1)
274+
for i := range f {
275+
f[i] = math.MaxInt32
276+
}
277+
f[0] = 0
278+
279+
calc := func(l, r int, rev bool) int {
280+
var cnt [26][26]int
281+
res := 0
282+
283+
for i := l; i <= r; i++ {
284+
j := i
285+
if rev {
286+
j = r - (i - l)
287+
}
288+
a := word1[j] - 'a'
289+
b := word2[i] - 'a'
290+
291+
if a != b {
292+
if cnt[b][a] > 0 {
293+
cnt[b][a]--
294+
} else {
295+
cnt[a][b]++
296+
res++
297+
}
298+
}
299+
}
300+
301+
return res
302+
}
303+
304+
for i := 1; i <= n; i++ {
305+
for j := 0; j < i; j++ {
306+
a := calc(j, i-1, false)
307+
b := 1 + calc(j, i-1, true)
308+
t := min(a, b)
309+
f[i] = min(f[i], f[j]+t)
310+
}
311+
}
312+
313+
return f[n]
314+
}
315+
```
172316

317+
#### TypeScript
318+
319+
```ts
320+
function minOperations(word1: string, word2: string): number {
321+
const n = word1.length;
322+
const f = Array(n + 1).fill(Number.MAX_SAFE_INTEGER);
323+
f[0] = 0;
324+
325+
function calc(l: number, r: number, rev: boolean): number {
326+
const cnt: number[][] = Array.from({ length: 26 }, () => Array(26).fill(0));
327+
let res = 0;
328+
329+
for (let i = l; i <= r; i++) {
330+
const j = rev ? r - (i - l) : i;
331+
const a = word1.charCodeAt(j) - 97;
332+
const b = word2.charCodeAt(i) - 97;
333+
334+
if (a !== b) {
335+
if (cnt[b][a] > 0) {
336+
cnt[b][a]--;
337+
} else {
338+
cnt[a][b]++;
339+
res++;
340+
}
341+
}
342+
}
343+
344+
return res;
345+
}
346+
347+
for (let i = 1; i <= n; i++) {
348+
for (let j = 0; j < i; j++) {
349+
const a = calc(j, i - 1, false);
350+
const b = 1 + calc(j, i - 1, true);
351+
const t = Math.min(a, b);
352+
f[i] = Math.min(f[i], f[j] + t);
353+
}
354+
}
355+
356+
return f[n];
357+
}
173358
```
174359

175360
<!-- tabs:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /