Skip to content

Commit d07de4b

Browse files
committed
feat: add solutions to lc problem: No.1347
No.1347.Minimum Number of Steps to Make Two Strings Anagram
1 parent 7379bd4 commit d07de4b

File tree

8 files changed

+122
-61
lines changed

8 files changed

+122
-61
lines changed

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

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ tags:
4646

4747
<pre><strong>输出:</strong>s = &quot;anagram&quot;, t = &quot;mangaar&quot;
4848
<strong>输出:</strong>0
49-
<strong>提示:</strong>&quot;anagram&quot;&quot;mangaar&quot; 本身就是一组字母异位词。
49+
<strong>提示:</strong>&quot;anagram&quot;&quot;mangaar&quot; 本身就是一组字母异位词。
5050
</pre>
5151

5252
<p><strong>示例 4:</strong></p>
@@ -77,13 +77,13 @@ tags:
7777

7878
<!-- solution:start -->
7979

80-
### 方法一:数组或哈希表
80+
### 方法一:计数
8181

82-
我们可以使用数组或哈希表 `cnt` 统计字符串 $s$ 中每个字符出现的次数,然后遍历字符串 $t$,对于 $t$ 中的每个字符,如果 $cnt$ 中对应的字符出现的次数大于 $0$,则将 $cnt$ 中对应的字符出现的次数减 $1$,否则将答案加 $1$
82+
我们可以使用一个哈希表或者数组 $\textit{cnt}$ 来统计字符串 $\textit{s}$ 中每个字符出现的次数,然后遍历字符串 $\textit{t}$,对于每个字符,我们在 $\textit{cnt}$ 中将其出现的次数减一,如果减一后的值小于 $0$,说明这个字符在字符串 $\textit{t}$ 中出现的次数比在字符串 $\textit{s}$ 中多,我们需要将这个字符替换掉,将答案加一
8383

84-
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度,而 $C$ 为字符集的大小。本题中 $C=26$
84+
遍历结束后,返回答案即可
8585

86-
<!-- tabs:start -->
86+
时间复杂度 $O(m + n)$,空间复杂度 $O(|\Sigma|)$,其中 $m$ 和 $n$ 分别是字符串 $\textit{s}$ 和 $\textit{t}$ 的长度,而 $|\Sigma|$ 是字符集的大小,本题中字符集为小写字母,因此 $|\Sigma| = 26$。
8787

8888
#### Python3
8989

@@ -93,10 +93,8 @@ class Solution:
9393
cnt = Counter(s)
9494
ans = 0
9595
for c in t:
96-
if cnt[c] > 0:
97-
cnt[c] -= 1
98-
else:
99-
ans += 1
96+
cnt[c] -= 1
97+
ans += cnt[c] < 0
10098
return ans
10199
```
102100

@@ -106,13 +104,13 @@ class Solution:
106104
class Solution {
107105
public int minSteps(String s, String t) {
108106
int[] cnt = new int[26];
109-
for (int i = 0; i < s.length(); ++i) {
110-
++cnt[s.charAt(i) - 'a'];
107+
for (char c : s.toCharArray()) {
108+
cnt[c - 'a']++;
111109
}
112110
int ans = 0;
113-
for (int i = 0; i < t.length(); ++i) {
114-
if (--cnt[t.charAt(i) - 'a'] < 0) {
115-
++ans;
111+
for (char c : t.toCharArray()) {
112+
if (--cnt[c - 'a'] < 0) {
113+
ans++;
116114
}
117115
}
118116
return ans;
@@ -127,10 +125,14 @@ class Solution {
127125
public:
128126
int minSteps(string s, string t) {
129127
int cnt[26]{};
130-
for (char& c : s) ++cnt[c - 'a'];
128+
for (char c : s) {
129+
++cnt[c - 'a'];
130+
}
131131
int ans = 0;
132-
for (char& c : t) {
133-
ans += --cnt[c - 'a'] < 0;
132+
for (char c : t) {
133+
if (--cnt[c - 'a'] < 0) {
134+
++ans;
135+
}
134136
}
135137
return ans;
136138
}
@@ -155,6 +157,24 @@ func minSteps(s string, t string) (ans int) {
155157
}
156158
```
157159

160+
#### TypeScript
161+
162+
```ts
163+
function minSteps(s: string, t: string): number {
164+
const cnt: number[] = Array(26).fill(0);
165+
for (const c of s) {
166+
++cnt[c.charCodeAt(0) - 97];
167+
}
168+
let ans = 0;
169+
for (const c of t) {
170+
if (--cnt[c.charCodeAt(0) - 97] < 0) {
171+
++ans;
172+
}
173+
}
174+
return ans;
175+
}
176+
```
177+
158178
#### JavaScript
159179

160180
```js
@@ -164,15 +184,15 @@ func minSteps(s string, t string) (ans int) {
164184
* @return {number}
165185
*/
166186
var minSteps = function (s, t) {
167-
const cnt = new Array(26).fill(0);
187+
const cnt = Array(26).fill(0);
168188
for (const c of s) {
169-
const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
170-
++cnt[i];
189+
++cnt[c.charCodeAt(0) - 97];
171190
}
172191
let ans = 0;
173192
for (const c of t) {
174-
const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
175-
ans += --cnt[i] < 0;
193+
if (--cnt[c.charCodeAt(0) - 97] < 0) {
194+
++ans;
195+
}
176196
}
177197
return ans;
178198
};

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

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ tags:
4848
<pre>
4949
<strong>Input:</strong> s = &quot;anagram&quot;, t = &quot;mangaar&quot;
5050
<strong>Output:</strong> 0
51-
<strong>Explanation:</strong> &quot;anagram&quot; and &quot;mangaar&quot; are anagrams.
51+
<strong>Explanation:</strong> &quot;anagram&quot; and &quot;mangaar&quot; are anagrams.
5252
</pre>
5353

5454
<p>&nbsp;</p>
@@ -66,7 +66,13 @@ tags:
6666

6767
<!-- solution:start -->
6868

69-
### Solution 1
69+
### Solution 1: Counting
70+
71+
We can use a hash table or an array $\textit{cnt}$ to count the occurrences of each character in the string $\textit{s}$. Then, we traverse the string $\textit{t}$. For each character, we decrement its count in $\textit{cnt}$. If the decremented value is less than $0$, it means that this character appears more times in the string $\textit{t}$ than in the string $\textit{s}$. In this case, we need to replace this character and increment the answer by one.
72+
73+
After the traversal, we return the answer.
74+
75+
The time complexity is $O(m + n)$, and the space complexity is $O(|\Sigma|)$, where $m$ and $n$ are the lengths of the strings $\textit{s}$ and $\textit{t}$, respectively, and $|\Sigma|$ is the size of the character set. In this problem, the character set consists of lowercase letters, so $|\Sigma| = 26$.
7076

7177
<!-- tabs:start -->
7278

@@ -78,10 +84,8 @@ class Solution:
7884
cnt = Counter(s)
7985
ans = 0
8086
for c in t:
81-
if cnt[c] > 0:
82-
cnt[c] -= 1
83-
else:
84-
ans += 1
87+
cnt[c] -= 1
88+
ans += cnt[c] < 0
8589
return ans
8690
```
8791

@@ -91,13 +95,13 @@ class Solution:
9195
class Solution {
9296
public int minSteps(String s, String t) {
9397
int[] cnt = new int[26];
94-
for (int i = 0; i < s.length(); ++i) {
95-
++cnt[s.charAt(i) - 'a'];
98+
for (char c : s.toCharArray()) {
99+
cnt[c - 'a']++;
96100
}
97101
int ans = 0;
98-
for (int i = 0; i < t.length(); ++i) {
99-
if (--cnt[t.charAt(i) - 'a'] < 0) {
100-
++ans;
102+
for (char c : t.toCharArray()) {
103+
if (--cnt[c - 'a'] < 0) {
104+
ans++;
101105
}
102106
}
103107
return ans;
@@ -112,10 +116,14 @@ class Solution {
112116
public:
113117
int minSteps(string s, string t) {
114118
int cnt[26]{};
115-
for (char& c : s) ++cnt[c - 'a'];
119+
for (char c : s) {
120+
++cnt[c - 'a'];
121+
}
116122
int ans = 0;
117-
for (char& c : t) {
118-
ans += --cnt[c - 'a'] < 0;
123+
for (char c : t) {
124+
if (--cnt[c - 'a'] < 0) {
125+
++ans;
126+
}
119127
}
120128
return ans;
121129
}
@@ -140,6 +148,24 @@ func minSteps(s string, t string) (ans int) {
140148
}
141149
```
142150

151+
#### TypeScript
152+
153+
```ts
154+
function minSteps(s: string, t: string): number {
155+
const cnt: number[] = Array(26).fill(0);
156+
for (const c of s) {
157+
++cnt[c.charCodeAt(0) - 97];
158+
}
159+
let ans = 0;
160+
for (const c of t) {
161+
if (--cnt[c.charCodeAt(0) - 97] < 0) {
162+
++ans;
163+
}
164+
}
165+
return ans;
166+
}
167+
```
168+
143169
#### JavaScript
144170

145171
```js
@@ -149,15 +175,15 @@ func minSteps(s string, t string) (ans int) {
149175
* @return {number}
150176
*/
151177
var minSteps = function (s, t) {
152-
const cnt = new Array(26).fill(0);
178+
const cnt = Array(26).fill(0);
153179
for (const c of s) {
154-
const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
155-
++cnt[i];
180+
++cnt[c.charCodeAt(0) - 97];
156181
}
157182
let ans = 0;
158183
for (const c of t) {
159-
const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
160-
ans += --cnt[i] < 0;
184+
if (--cnt[c.charCodeAt(0) - 97] < 0) {
185+
++ans;
186+
}
161187
}
162188
return ans;
163189
};

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ class Solution {
22
public:
33
int minSteps(string s, string t) {
44
int cnt[26]{};
5-
for (char& c : s) ++cnt[c - 'a'];
5+
for (char c : s) {
6+
++cnt[c - 'a'];
7+
}
68
int ans = 0;
7-
for (char& c : t) {
8-
ans += --cnt[c - 'a'] < 0;
9+
for (char c : t) {
10+
if (--cnt[c - 'a'] < 0) {
11+
++ans;
12+
}
913
}
1014
return ans;
1115
}
12-
};
16+
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ func minSteps(s string, t string) (ans int) {
1010
}
1111
}
1212
return
13-
}
13+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution {
22
public int minSteps(String s, String t) {
33
int[] cnt = new int[26];
4-
for (int i = 0; i < s.length(); ++i) {
5-
++cnt[s.charAt(i) - 'a'];
4+
for (char c : s.toCharArray()) {
5+
cnt[c - 'a']++;
66
}
77
int ans = 0;
8-
for (int i = 0; i < t.length(); ++i) {
9-
if (--cnt[t.charAt(i) - 'a'] < 0) {
10-
++ans;
8+
for (char c : t.toCharArray()) {
9+
if (--cnt[c - 'a'] < 0) {
10+
ans++;
1111
}
1212
}
1313
return ans;
1414
}
15-
}
15+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* @return {number}
55
*/
66
var minSteps = function (s, t) {
7-
const cnt = new Array(26).fill(0);
7+
const cnt = Array(26).fill(0);
88
for (const c of s) {
9-
const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
10-
++cnt[i];
9+
++cnt[c.charCodeAt(0) - 97];
1110
}
1211
let ans = 0;
1312
for (const c of t) {
14-
const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
15-
ans += --cnt[i] < 0;
13+
if (--cnt[c.charCodeAt(0) - 97] < 0) {
14+
++ans;
15+
}
1616
}
1717
return ans;
1818
};

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ def minSteps(self, s: str, t: str) -> int:
33
cnt = Counter(s)
44
ans = 0
55
for c in t:
6-
if cnt[c] > 0:
7-
cnt[c] -= 1
8-
else:
9-
ans += 1
6+
cnt[c] -= 1
7+
ans += cnt[c] < 0
108
return ans
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function minSteps(s: string, t: string): number {
2+
const cnt: number[] = Array(26).fill(0);
3+
for (const c of s) {
4+
++cnt[c.charCodeAt(0) - 97];
5+
}
6+
let ans = 0;
7+
for (const c of t) {
8+
if (--cnt[c.charCodeAt(0) - 97] < 0) {
9+
++ans;
10+
}
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
 (0)