@@ -93,11 +93,18 @@ class Solution {
9393class Solution {
9494public:
9595 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+ }
101108 return true;
102109 }
103110};
@@ -115,8 +122,7 @@ func CheckPermutation(s1 string, s2 string) bool {
115122 cnt[c-'a']++
116123 }
117124 for _, c := range s2 {
118- cnt[c-'a']--
119- if cnt[c-'a'] < 0 {
125+ if cnt[c-'a']--; cnt[c-'a'] < 0 {
120126 return false
121127 }
122128 }
@@ -128,20 +134,18 @@ func CheckPermutation(s1 string, s2 string) bool {
128134
129135``` ts
130136function 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 ) {
134138 return false ;
135139 }
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 ;
140143 }
141- for (const v of map . values () ) {
142- if (v !== 0 ) {
144+ for (const c of s2 ) {
145+ if (! cnt [ c ] ) {
143146 return false ;
144147 }
148+ cnt [c ]-- ;
145149 }
146150 return true ;
147151}
@@ -150,22 +154,26 @@ function CheckPermutation(s1: string, s2: string): boolean {
150154#### Rust
151155
152156``` rust
153- use std :: collections :: HashMap ;
154157impl Solution {
155158 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 () {
159160 return false ;
160161 }
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 ;
167174 }
168- map . values (). all (| i | * i == 0 )
175+
176+ true
169177 }
170178}
171179```
@@ -179,19 +187,18 @@ impl Solution {
179187 * @return {boolean}
180188 */
181189var CheckPermutation = function (s1 , s2 ) {
182- if (s1 .length != s2 .length ) {
190+ if (s1 .length !== s2 .length ) {
183191 return false ;
184192 }
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 ;
189196 }
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]) {
193199 return false ;
194200 }
201+ cnt[c]-- ;
195202 }
196203 return true ;
197204};
@@ -206,19 +213,18 @@ class Solution {
206213 return false
207214 }
208215
209- var cnt = Array (repeating : 0 , count : 26 )
216+ var cnt = [ Int ] (repeating : 0 , count : 26 )
210217
211218 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
214220 }
215221
216222 for char in s2 {
217223 let index = Int (char.asciiValue ! - Character (" a" ).asciiValue ! )
218- cnt[index] -= 1
219- if cnt[index] < 0 {
224+ if cnt[index] == 0 {
220225 return false
221226 }
227+ cnt[index] -= 1
222228 }
223229
224230 return true
@@ -268,8 +274,8 @@ class Solution {
268274class Solution {
269275public:
270276 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);
273279 return s1 == s2;
274280 }
275281};
@@ -308,6 +314,31 @@ impl Solution {
308314}
309315```
310316
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+
311342<!-- tabs: end -->
312343
313344<!-- solution: end -->
0 commit comments