Skip to content

feat: add solutions to lc problem: No.3579 #4481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,210 @@ tags:
#### Python3

```python

class Solution:
def minOperations(self, word1: str, word2: str) -> int:
def calc(l: int, r: int, rev: bool) -> int:
cnt = Counter()
res = 0
for i in range(l, r + 1):
j = r - (i - l) if rev else i
a, b = word1[j], word2[i]
if a != b:
if cnt[(b, a)] > 0:
cnt[(b, a)] -= 1
else:
cnt[(a, b)] += 1
res += 1
return res

n = len(word1)
f = [inf] * (n + 1)
f[0] = 0
for i in range(1, n + 1):
for j in range(i):
t = min(calc(j, i - 1, False), 1 + calc(j, i - 1, True))
f[i] = min(f[i], f[j] + t)
return f[n]
```

#### Java

```java

class Solution {
public int minOperations(String word1, String word2) {
int n = word1.length();
int[] f = new int[n + 1];
Arrays.fill(f, Integer.MAX_VALUE);
f[0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
int a = calc(word1, word2, j, i - 1, false);
int b = 1 + calc(word1, word2, j, i - 1, true);
int t = Math.min(a, b);
f[i] = Math.min(f[i], f[j] + t);
}
}
return f[n];
}

private int calc(String word1, String word2, int l, int r, boolean rev) {
int[][] cnt = new int[26][26];
int res = 0;
for (int i = l; i <= r; i++) {
int j = rev ? r - (i - l) : i;
int a = word1.charAt(j) - 'a';
int b = word2.charAt(i) - 'a';
if (a != b) {
if (cnt[b][a] > 0) {
cnt[b][a]--;
} else {
cnt[a][b]++;
res++;
}
}
}
return res;
}
}
```

#### C++

```cpp

class Solution {
public:
int minOperations(string word1, string word2) {
int n = word1.length();
vector<int> f(n + 1, INT_MAX);
f[0] = 0;

for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
int a = calc(word1, word2, j, i - 1, false);
int b = 1 + calc(word1, word2, j, i - 1, true);
int t = min(a, b);
f[i] = min(f[i], f[j] + t);
}
}

return f[n];
}

private:
int calc(const string& word1, const string& word2, int l, int r, bool rev) {
int cnt[26][26] = {0};
int res = 0;

for (int i = l; i <= r; ++i) {
int j = rev ? r - (i - l) : i;
int a = word1[j] - 'a';
int b = word2[i] - 'a';

if (a != b) {
if (cnt[b][a] > 0) {
cnt[b][a]--;
} else {
cnt[a][b]++;
res++;
}
}
}

return res;
}
};
```

#### Go

```go
func minOperations(word1 string, word2 string) int {
n := len(word1)
f := make([]int, n+1)
for i := range f {
f[i] = math.MaxInt32
}
f[0] = 0

calc := func(l, r int, rev bool) int {
var cnt [26][26]int
res := 0

for i := l; i <= r; i++ {
j := i
if rev {
j = r - (i - l)
}
a := word1[j] - 'a'
b := word2[i] - 'a'

if a != b {
if cnt[b][a] > 0 {
cnt[b][a]--
} else {
cnt[a][b]++
res++
}
}
}

return res
}

for i := 1; i <= n; i++ {
for j := 0; j < i; j++ {
a := calc(j, i-1, false)
b := 1 + calc(j, i-1, true)
t := min(a, b)
f[i] = min(f[i], f[j]+t)
}
}

return f[n]
}
```

#### TypeScript

```ts
function minOperations(word1: string, word2: string): number {
const n = word1.length;
const f = Array(n + 1).fill(Number.MAX_SAFE_INTEGER);
f[0] = 0;

function calc(l: number, r: number, rev: boolean): number {
const cnt: number[][] = Array.from({ length: 26 }, () => Array(26).fill(0));
let res = 0;

for (let i = l; i <= r; i++) {
const j = rev ? r - (i - l) : i;
const a = word1.charCodeAt(j) - 97;
const b = word2.charCodeAt(i) - 97;

if (a !== b) {
if (cnt[b][a] > 0) {
cnt[b][a]--;
} else {
cnt[a][b]++;
res++;
}
}
}

return res;
}

for (let i = 1; i <= n; i++) {
for (let j = 0; j < i; j++) {
const a = calc(j, i - 1, false);
const b = 1 + calc(j, i - 1, true);
const t = Math.min(a, b);
f[i] = Math.min(f[i], f[j] + t);
}
}

return f[n];
}
```

<!-- tabs:end -->
Expand Down
Loading