File tree 3 files changed +25
-2
lines changed
src/chapters/01/exercises/check-permutation
3 files changed +25
-2
lines changed Original file line number Diff line number Diff line change 1
1
/* Book solutions */
2
2
const { } = require ( "./book-solutions" ) ;
3
3
/* My solutions */
4
- const { v1 } = require ( "./my-solutions" ) ;
4
+ const { v1, v2 } = require ( "./my-solutions" ) ;
5
5
6
6
/* Export the solution to be tested */
7
- module . exports = v1 ;
7
+ module . exports = v2 ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Sorting both strings and compare.
3
+ * Running time: Depends on the sorting algorithm.
4
+ * @param {string } str1
5
+ * @param {string } str2
6
+ * @returns {boolean }
7
+ */
8
+ function checkPermutation ( str1 , str2 ) {
9
+ // if lengths are different, then permutation is not possible.
10
+ if ( str1 . length !== str2 . length ) return false ;
11
+
12
+ const sort1 = str1 . split ( "" ) . sort ( ) ;
13
+ const sort2 = str2 . split ( "" ) . sort ( ) ;
14
+
15
+ for ( let i = 0 ; i < sort1 . length ; i ++ ) {
16
+ if ( sort1 [ i ] !== sort2 [ i ] ) return false ;
17
+ }
18
+
19
+ return true ;
20
+ }
21
+
22
+ module . exports = checkPermutation ;
Original file line number Diff line number Diff line change 1
1
module . exports = {
2
2
v1 : require ( "./check-permutation.v1" ) ,
3
+ v2 : require ( "./check-permutation.v2" ) ,
3
4
} ;
You can’t perform that action at this time.
0 commit comments