Skip to content

Commit e3abeb2

Browse files
author
Isaac Ramirez
committed
Solve exercise 1.2 by sorting the strings.
1 parent cca55be commit e3abeb2

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* Book solutions */
22
const {} = require("./book-solutions");
33
/* My solutions */
4-
const { v1 } = require("./my-solutions");
4+
const { v1, v2 } = require("./my-solutions");
55

66
/* Export the solution to be tested */
7-
module.exports = v1;
7+
module.exports = v2;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
v1: require("./check-permutation.v1"),
3+
v2: require("./check-permutation.v2"),
34
};

0 commit comments

Comments
 (0)