Skip to content

Commit 6e4b052

Browse files
committed
feat: add solutions to lc problem: No.2186
No.2186.Minimum Number of Steps to Make Two Strings Anagram II
1 parent 3d022be commit 6e4b052

File tree

8 files changed

+272
-28
lines changed

8 files changed

+272
-28
lines changed

solution/2100-2199/2186.Minimum Number of Steps to Make Two Strings Anagram II/README.md

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,131 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:计数**
49+
4850
<!-- tabs:start -->
4951

5052
### **Python3**
5153

5254
<!-- 这里可写当前语言的特殊实现逻辑 -->
5355

5456
```python
55-
57+
class Solution:
58+
def minSteps(self, s: str, t: str) -> int:
59+
cnt = Counter(s)
60+
for c in t:
61+
cnt[c] -= 1
62+
return sum(abs(v) for v in cnt.values())
5663
```
5764

5865
### **Java**
5966

6067
<!-- 这里可写当前语言的特殊实现逻辑 -->
6168

6269
```java
63-
70+
class Solution {
71+
public int minSteps(String s, String t) {
72+
int[] cnt = new int[26];
73+
for (char c : s.toCharArray()) {
74+
++cnt[c - 'a'];
75+
}
76+
for (char c : t.toCharArray()) {
77+
--cnt[c - 'a'];
78+
}
79+
int ans = 0;
80+
for (int v : cnt) {
81+
ans += Math.abs(v);
82+
}
83+
return ans;
84+
}
85+
}
6486
```
6587

6688
### **TypeScript**
6789

6890
```ts
6991
function minSteps(s: string, t: string): number {
70-
let count1 = new Array(128).fill(0);
71-
let count2 = new Array(128).fill(0);
72-
for (let char of s) {
73-
count1[char.charCodeAt(0)]++;
92+
let cnt = new Array(128).fill(0);
93+
for (const c of s) {
94+
++cnt[c.charCodeAt(0)];
7495
}
75-
for (let char of t) {
76-
count2[char.charCodeAt(0)]++;
96+
for (const c of t) {
97+
--cnt[c.charCodeAt(0)];
7798
}
7899
let ans = 0;
79-
for (let i = 0; i < 128; i++) {
80-
ans += Math.abs(count1[i] - count2[i]);
100+
for (const v of cnt) {
101+
ans += Math.abs(v);
81102
}
82103
return ans;
83104
}
84105
```
85106

107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int minSteps(string s, string t) {
113+
vector<int> cnt(26);
114+
for (char& c : s) ++cnt[c - 'a'];
115+
for (char& c : t) --cnt[c - 'a'];
116+
int ans = 0;
117+
for (int& v : cnt) ans += abs(v);
118+
return ans;
119+
}
120+
};
121+
```
122+
123+
### **Go**
124+
125+
```go
126+
func minSteps(s string, t string) int {
127+
cnt := make([]int, 26)
128+
for _, c := range s {
129+
cnt[c-'a']++
130+
}
131+
for _, c := range t {
132+
cnt[c-'a']--
133+
}
134+
ans := 0
135+
for _, v := range cnt {
136+
ans += abs(v)
137+
}
138+
return ans
139+
}
140+
141+
func abs(x int) int {
142+
if x < 0 {
143+
return -x
144+
}
145+
return x
146+
}
147+
```
148+
149+
### **JavaScript**
150+
151+
```js
152+
/**
153+
* @param {string} s
154+
* @param {string} t
155+
* @return {number}
156+
*/
157+
var minSteps = function (s, t) {
158+
let cnt = new Array(26).fill(0);
159+
for (const c of s) {
160+
++cnt[c.charCodeAt() - 'a'.charCodeAt()];
161+
}
162+
for (const c of t) {
163+
--cnt[c.charCodeAt() - 'a'.charCodeAt()];
164+
}
165+
let ans = 0;
166+
for (const v of cnt) {
167+
ans += Math.abs(v);
168+
}
169+
return ans;
170+
};
171+
````
172+
86173
### **...**
87174

88175
```

solution/2100-2199/2186.Minimum Number of Steps to Make Two Strings Anagram II/README_EN.md

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,120 @@ It can be shown that there is no way to make them anagrams of each other with le
4747
### **Python3**
4848

4949
```python
50-
50+
class Solution:
51+
def minSteps(self, s: str, t: str) -> int:
52+
cnt = Counter(s)
53+
for c in t:
54+
cnt[c] -= 1
55+
return sum(abs(v) for v in cnt.values())
5156
```
5257

5358
### **Java**
5459

5560
```java
56-
61+
class Solution {
62+
public int minSteps(String s, String t) {
63+
int[] cnt = new int[26];
64+
for (char c : s.toCharArray()) {
65+
++cnt[c - 'a'];
66+
}
67+
for (char c : t.toCharArray()) {
68+
--cnt[c - 'a'];
69+
}
70+
int ans = 0;
71+
for (int v : cnt) {
72+
ans += Math.abs(v);
73+
}
74+
return ans;
75+
}
76+
}
5777
```
5878

5979
### **TypeScript**
6080

6181
```ts
6282
function minSteps(s: string, t: string): number {
63-
let count1 = new Array(128).fill(0);
64-
let count2 = new Array(128).fill(0);
65-
for (let char of s) {
66-
count1[char.charCodeAt(0)]++;
83+
let cnt = new Array(128).fill(0);
84+
for (const c of s) {
85+
++cnt[c.charCodeAt(0)];
6786
}
68-
for (let char of t) {
69-
count2[char.charCodeAt(0)]++;
87+
for (const c of t) {
88+
--cnt[c.charCodeAt(0)];
7089
}
7190
let ans = 0;
72-
for (let i = 0; i < 128; i++) {
73-
ans += Math.abs(count1[i] - count2[i]);
91+
for (const v of cnt) {
92+
ans += Math.abs(v);
7493
}
7594
return ans;
7695
}
7796
```
7897

98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int minSteps(string s, string t) {
104+
vector<int> cnt(26);
105+
for (char& c : s) ++cnt[c - 'a'];
106+
for (char& c : t) --cnt[c - 'a'];
107+
int ans = 0;
108+
for (int& v : cnt) ans += abs(v);
109+
return ans;
110+
}
111+
};
112+
```
113+
114+
### **Go**
115+
116+
```go
117+
func minSteps(s string, t string) int {
118+
cnt := make([]int, 26)
119+
for _, c := range s {
120+
cnt[c-'a']++
121+
}
122+
for _, c := range t {
123+
cnt[c-'a']--
124+
}
125+
ans := 0
126+
for _, v := range cnt {
127+
ans += abs(v)
128+
}
129+
return ans
130+
}
131+
132+
func abs(x int) int {
133+
if x < 0 {
134+
return -x
135+
}
136+
return x
137+
}
138+
```
139+
140+
### **JavaScript**
141+
142+
```js
143+
/**
144+
* @param {string} s
145+
* @param {string} t
146+
* @return {number}
147+
*/
148+
var minSteps = function (s, t) {
149+
let cnt = new Array(26).fill(0);
150+
for (const c of s) {
151+
++cnt[c.charCodeAt() - 'a'.charCodeAt()];
152+
}
153+
for (const c of t) {
154+
--cnt[c.charCodeAt() - 'a'.charCodeAt()];
155+
}
156+
let ans = 0;
157+
for (const v of cnt) {
158+
ans += Math.abs(v);
159+
}
160+
return ans;
161+
};
162+
````
163+
79164
### **...**
80165

81166
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int minSteps(string s, string t) {
4+
vector<int> cnt(26);
5+
for (char& c : s) ++cnt[c - 'a'];
6+
for (char& c : t) --cnt[c - 'a'];
7+
int ans = 0;
8+
for (int& v : cnt) ans += abs(v);
9+
return ans;
10+
}
11+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func minSteps(s string, t string) int {
2+
cnt := make([]int, 26)
3+
for _, c := range s {
4+
cnt[c-'a']++
5+
}
6+
for _, c := range t {
7+
cnt[c-'a']--
8+
}
9+
ans := 0
10+
for _, v := range cnt {
11+
ans += abs(v)
12+
}
13+
return ans
14+
}
15+
16+
func abs(x int) int {
17+
if x < 0 {
18+
return -x
19+
}
20+
return x
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int minSteps(String s, String t) {
3+
int[] cnt = new int[26];
4+
for (char c : s.toCharArray()) {
5+
++cnt[c - 'a'];
6+
}
7+
for (char c : t.toCharArray()) {
8+
--cnt[c - 'a'];
9+
}
10+
int ans = 0;
11+
for (int v : cnt) {
12+
ans += Math.abs(v);
13+
}
14+
return ans;
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {number}
5+
*/
6+
var minSteps = function (s, t) {
7+
let cnt = new Array(26).fill(0);
8+
for (const c of s) {
9+
++cnt[c.charCodeAt() - 'a'.charCodeAt()];
10+
}
11+
for (const c of t) {
12+
--cnt[c.charCodeAt() - 'a'.charCodeAt()];
13+
}
14+
let ans = 0;
15+
for (const v of cnt) {
16+
ans += Math.abs(v);
17+
}
18+
return ans;
19+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def minSteps(self, s: str, t: str) -> int:
3+
cnt = Counter(s)
4+
for c in t:
5+
cnt[c] -= 1
6+
return sum(abs(v) for v in cnt.values())
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
function minSteps(s: string, t: string): number {
2-
let count1 = new Array(128).fill(0);
3-
let count2 = new Array(128).fill(0);
4-
for (let char of s) {
5-
count1[char.charCodeAt(0)]++;
2+
let cnt = new Array(128).fill(0);
3+
for (const c of s) {
4+
++cnt[c.charCodeAt(0)];
65
}
7-
for (let char of t) {
8-
count2[char.charCodeAt(0)]++;
6+
for (const c of t) {
7+
--cnt[c.charCodeAt(0)];
98
}
109
let ans = 0;
11-
for (let i = 0; i < 128; i++) {
12-
ans += Math.abs(count1[i] - count2[i]);
10+
for (const v of cnt) {
11+
ans += Math.abs(v);
1312
}
1413
return ans;
1514
}

0 commit comments

Comments
 (0)