@@ -93,11 +93,18 @@ class Solution {
93
93
class Solution {
94
94
public:
95
95
bool CheckPermutation(string s1, string s2) {
96
- if (s1.size() != s2.size()) return false;
97
- int cnt[ 26] = {0};
98
- for (char& c : s1) ++cnt[ c - 'a'] ;
99
- for (char& c : s2)
100
- if (--cnt[ c - 'a'] < 0) return false;
96
+ if (s1.size() != s2.size()) {
97
+ return false;
98
+ }
99
+ int cnt[ 26] {};
100
+ for (char c : s1) {
101
+ ++cnt[ c - 'a'] ;
102
+ }
103
+ for (char c : s2) {
104
+ if (--cnt[ c - 'a'] < 0) {
105
+ return false;
106
+ }
107
+ }
101
108
return true;
102
109
}
103
110
};
@@ -115,8 +122,7 @@ func CheckPermutation(s1 string, s2 string) bool {
115
122
cnt[c-'a']++
116
123
}
117
124
for _, c := range s2 {
118
- cnt[c-'a']--
119
- if cnt[c-'a'] < 0 {
125
+ if cnt[c-'a']--; cnt[c-'a'] < 0 {
120
126
return false
121
127
}
122
128
}
@@ -128,20 +134,18 @@ func CheckPermutation(s1 string, s2 string) bool {
128
134
129
135
``` ts
130
136
function CheckPermutation(s1 : string , s2 : string ): boolean {
131
- const n = s1 .length ;
132
- const m = s2 .length ;
133
- if (n !== m ) {
137
+ if (s1 .length !== s2 .length ) {
134
138
return false ;
135
139
}
136
- const map = new Map <string , number >();
137
- for (let i = 0 ; i < n ; i ++ ) {
138
- map .set (s1 [i ], (map .get (s1 [i ]) ?? 0 ) + 1 );
139
- map .set (s2 [i ], (map .get (s2 [i ]) ?? 0 ) - 1 );
140
+ const cnt: Record <string , number > = {};
141
+ for (const c of s1 ) {
142
+ cnt [c ] = (cnt [c ] || 0 ) + 1 ;
140
143
}
141
- for (const v of map . values () ) {
142
- if (v !== 0 ) {
144
+ for (const c of s2 ) {
145
+ if (! cnt [ c ] ) {
143
146
return false ;
144
147
}
148
+ cnt [c ]-- ;
145
149
}
146
150
return true ;
147
151
}
@@ -150,22 +154,26 @@ function CheckPermutation(s1: string, s2: string): boolean {
150
154
#### Rust
151
155
152
156
``` rust
153
- use std :: collections :: HashMap ;
154
157
impl Solution {
155
158
pub fn check_permutation (s1 : String , s2 : String ) -> bool {
156
- let n = s1 . len ();
157
- let m = s2 . len ();
158
- if n != m {
159
+ if s1 . len () != s2 . len () {
159
160
return false ;
160
161
}
161
- let s1 = s1 . as_bytes ();
162
- let s2 = s2 . as_bytes ();
163
- let mut map = HashMap :: new ();
164
- for i in 0 .. n {
165
- * map . entry (s1 [i ]). or_insert (0 ) += 1 ;
166
- * map . entry (s2 [i ]). or_insert (0 ) -= 1 ;
162
+
163
+ let mut cnt = vec! [0 ; 26 ];
164
+ for c in s1 . chars () {
165
+ cnt [(c as usize - 'a' as usize )] += 1 ;
166
+ }
167
+
168
+ for c in s2 . chars () {
169
+ let index = c as usize - 'a' as usize ;
170
+ if cnt [index ] == 0 {
171
+ return false ;
172
+ }
173
+ cnt [index ] -= 1 ;
167
174
}
168
- map . values (). all (| i | * i == 0 )
175
+
176
+ true
169
177
}
170
178
}
171
179
```
@@ -179,19 +187,18 @@ impl Solution {
179
187
* @return {boolean}
180
188
*/
181
189
var CheckPermutation = function (s1 , s2 ) {
182
- if (s1 .length != s2 .length ) {
190
+ if (s1 .length !== s2 .length ) {
183
191
return false ;
184
192
}
185
- const cnt = new Array (26 ).fill (0 );
186
- for (let i = 0 ; i < s1 .length ; ++ i) {
187
- const j = s1 .codePointAt (i) - ' a' .codePointAt (0 );
188
- ++ cnt[j];
193
+ const cnt = {};
194
+ for (const c of s1) {
195
+ cnt[c] = (cnt[c] || 0 ) + 1 ;
189
196
}
190
- for (let i = 0 ; i < s2 .length ; ++ i) {
191
- const j = s2 .codePointAt (i) - ' a' .codePointAt (0 );
192
- if (-- cnt[j] < 0 ) {
197
+ for (const c of s2) {
198
+ if (! cnt[c]) {
193
199
return false ;
194
200
}
201
+ cnt[c]-- ;
195
202
}
196
203
return true ;
197
204
};
@@ -206,19 +213,18 @@ class Solution {
206
213
return false
207
214
}
208
215
209
- var cnt = Array (repeating : 0 , count : 26 )
216
+ var cnt = [ Int ] (repeating : 0 , count : 26 )
210
217
211
218
for char in s1 {
212
- let index = Int (char.asciiValue ! - Character (" a" ).asciiValue ! )
213
- cnt[index] += 1
219
+ cnt[Int (char.asciiValue ! - Character (" a" ).asciiValue ! )] += 1
214
220
}
215
221
216
222
for char in s2 {
217
223
let index = Int (char.asciiValue ! - Character (" a" ).asciiValue ! )
218
- cnt[index] -= 1
219
- if cnt[index] < 0 {
224
+ if cnt[index] == 0 {
220
225
return false
221
226
}
227
+ cnt[index] -= 1
222
228
}
223
229
224
230
return true
@@ -268,8 +274,8 @@ class Solution {
268
274
class Solution {
269
275
public:
270
276
bool CheckPermutation(string s1, string s2) {
271
- sort(s1.begin(), s1.end() );
272
- sort(s2.begin(), s2.end() );
277
+ ranges:: sort(s1);
278
+ ranges:: sort(s2);
273
279
return s1 == s2;
274
280
}
275
281
};
@@ -308,6 +314,31 @@ impl Solution {
308
314
}
309
315
```
310
316
317
+ #### JavaScript
318
+
319
+ ``` js
320
+ /**
321
+ * @param {string} s1
322
+ * @param {string} s2
323
+ * @return {boolean}
324
+ */
325
+ var CheckPermutation = function (s1 , s2 ) {
326
+ return [... s1].sort ().join (' ' ) === [... s2].sort ().join (' ' );
327
+ };
328
+ ```
329
+
330
+ #### Swift
331
+
332
+ ``` swift
333
+ class Solution {
334
+ func CheckPermutation (_ s1 : String , _ s2 : String ) -> Bool {
335
+ let s1 = s1.sorted ()
336
+ let s2 = s2.sorted ()
337
+ return s1 == s2
338
+ }
339
+ }
340
+ ```
341
+
311
342
<!-- tabs: end -->
312
343
313
344
<!-- solution: end -->
0 commit comments