File tree Expand file tree Collapse file tree 6 files changed +180
-15
lines changed
solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram Expand file tree Collapse file tree 6 files changed +180
-15
lines changed Original file line number Diff line number Diff line change 57
57
<li><code>s</code> 和 <code>t</code> 只包含小写英文字母</li>
58
58
</ul >
59
59
60
-
61
60
## 解法
62
61
63
62
<!-- 这里可写通用的实现逻辑 -->
64
63
64
+ “哈希表”实现。
65
+
65
66
<!-- tabs:start -->
66
67
67
68
### ** Python3**
68
69
69
70
<!-- 这里可写当前语言的特殊实现逻辑 -->
70
71
71
72
``` 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
73
83
```
74
84
75
85
### ** Java**
76
86
77
87
<!-- 这里可写当前语言的特殊实现逻辑 -->
78
88
79
89
``` 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
+ ```
80
127
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
+ }
81
146
```
82
147
83
148
### ** ...**
Original file line number Diff line number Diff line change 58
58
<li><code>s</code> and <code>t</code> contain lower-case English letters only.</li>
59
59
</ul >
60
60
61
-
62
61
## Solutions
63
62
64
63
<!-- tabs:start -->
65
64
66
65
### ** Python3**
67
66
68
67
``` 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
70
78
```
71
79
72
80
### ** Java**
73
81
74
82
``` 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
+ ```
75
120
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
+ }
76
139
```
77
140
78
141
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- import java .util .*;
2
-
3
1
class Solution {
4
2
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' ];
10
6
}
11
7
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
+ }
16
14
}
17
15
return res ;
18
16
}
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments