Skip to content

Commit bb19e78

Browse files
committed
feat: add solutions to lc problems: No.1347.Minimum Number of Steps to Make Two Strings Anagram
1 parent d6db065 commit bb19e78

File tree

6 files changed

+180
-15
lines changed

6 files changed

+180
-15
lines changed

solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,92 @@
5757
<li><code>s</code> 和 <code>t</code>&nbsp;只包含小写英文字母</li>
5858
</ul>
5959

60-
6160
## 解法
6261

6362
<!-- 这里可写通用的实现逻辑 -->
6463

64+
“哈希表”实现。
65+
6566
<!-- tabs:start -->
6667

6768
### **Python3**
6869

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

7172
```python
72-
73+
class Solution:
74+
def minSteps(self, s: str, t: str) -> int:
75+
counter = collections.Counter(s)
76+
res = 0
77+
for c in t:
78+
if counter[c] > 0:
79+
counter[c] -= 1
80+
else:
81+
res += 1
82+
return res
7383
```
7484

7585
### **Java**
7686

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

7989
```java
90+
class Solution {
91+
public int minSteps(String s, String t) {
92+
int[] counter = new int[26];
93+
for (char c : s.toCharArray()) {
94+
++counter[c - 'a'];
95+
}
96+
int res = 0;
97+
for (char c : t.toCharArray()) {
98+
if (counter[c - 'a'] > 0) {
99+
--counter[c - 'a'];
100+
} else {
101+
++res;
102+
}
103+
}
104+
return res;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int minSteps(string s, string t) {
115+
vector<int> counter(26);
116+
for (char c : s) ++counter[c - 'a'];
117+
int res = 0;
118+
for (char c : t)
119+
{
120+
if (counter[c - 'a'] > 0) --counter[c - 'a'];
121+
else ++res;
122+
}
123+
return res;
124+
}
125+
};
126+
```
80127
128+
### **Go**
129+
130+
```go
131+
func minSteps(s string, t string) int {
132+
counter := make([]int, 26)
133+
for _, c := range s {
134+
counter[c-'a']++
135+
}
136+
res := 0
137+
for _, c := range t {
138+
if counter[c-'a'] > 0 {
139+
counter[c-'a']--
140+
} else {
141+
res++
142+
}
143+
}
144+
return res
145+
}
81146
```
82147

83148
### **...**

solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README_EN.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,84 @@
5858
<li><code>s</code> and <code>t</code> contain lower-case English letters only.</li>
5959
</ul>
6060

61-
6261
## Solutions
6362

6463
<!-- tabs:start -->
6564

6665
### **Python3**
6766

6867
```python
69-
68+
class Solution:
69+
def minSteps(self, s: str, t: str) -> int:
70+
counter = collections.Counter(s)
71+
res = 0
72+
for c in t:
73+
if counter[c] > 0:
74+
counter[c] -= 1
75+
else:
76+
res += 1
77+
return res
7078
```
7179

7280
### **Java**
7381

7482
```java
83+
class Solution {
84+
public int minSteps(String s, String t) {
85+
int[] counter = new int[26];
86+
for (char c : s.toCharArray()) {
87+
++counter[c - 'a'];
88+
}
89+
int res = 0;
90+
for (char c : t.toCharArray()) {
91+
if (counter[c - 'a'] > 0) {
92+
--counter[c - 'a'];
93+
} else {
94+
++res;
95+
}
96+
}
97+
return res;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int minSteps(string s, string t) {
108+
vector<int> counter(26);
109+
for (char c : s) ++counter[c - 'a'];
110+
int res = 0;
111+
for (char c : t)
112+
{
113+
if (counter[c - 'a'] > 0) --counter[c - 'a'];
114+
else ++res;
115+
}
116+
return res;
117+
}
118+
};
119+
```
75120
121+
### **Go**
122+
123+
```go
124+
func minSteps(s string, t string) int {
125+
counter := make([]int, 26)
126+
for _, c := range s {
127+
counter[c-'a']++
128+
}
129+
res := 0
130+
for _, c := range t {
131+
if counter[c-'a'] > 0 {
132+
counter[c-'a']--
133+
} else {
134+
res++
135+
}
136+
}
137+
return res
138+
}
76139
```
77140

78141
### **...**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int minSteps(string s, string t) {
4+
vector<int> counter(26);
5+
for (char c : s) ++counter[c - 'a'];
6+
int res = 0;
7+
for (char c : t)
8+
{
9+
if (counter[c - 'a'] > 0) --counter[c - 'a'];
10+
else ++res;
11+
}
12+
return res;
13+
}
14+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func minSteps(s string, t string) int {
2+
counter := make([]int, 26)
3+
for _, c := range s {
4+
counter[c-'a']++
5+
}
6+
res := 0
7+
for _, c := range t {
8+
if counter[c-'a'] > 0 {
9+
counter[c-'a']--
10+
} else {
11+
res++
12+
}
13+
}
14+
return res
15+
}

solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import java.util.*;
2-
31
class Solution {
42
public int minSteps(String s, String t) {
5-
Map<Character, Integer> map = new HashMap<>();
6-
for (char c : t.toCharArray()) {
7-
if (map.containsKey(c)) {
8-
map.put(c, map.get(c) + 1);
9-
} else map.put(c, 1);
3+
int[] counter = new int[26];
4+
for (char c : s.toCharArray()) {
5+
++counter[c - 'a'];
106
}
117
int res = 0;
12-
for (char c : s.toCharArray()) {
13-
if (map.containsKey(c) && map.get(c) > 0) {
14-
map.put(c, map.get(c) - 1);
15-
} else res ++;
8+
for (char c : t.toCharArray()) {
9+
if (counter[c - 'a'] > 0) {
10+
--counter[c - 'a'];
11+
} else {
12+
++res;
13+
}
1614
}
1715
return res;
1816
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def minSteps(self, s: str, t: str) -> int:
3+
counter = collections.Counter(s)
4+
res = 0
5+
for c in t:
6+
if counter[c] > 0:
7+
counter[c] -= 1
8+
else:
9+
res += 1
10+
return res

0 commit comments

Comments
 (0)