Skip to content

Commit c555454

Browse files
author
Kyle Maune
committed
added second solution for permutation step
1 parent 5963760 commit c555454

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Medium Difficulty/permutationStep2.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// coderbyte solution for permutation step
2+
// splashinn
3+
4+
function permutationStep(num) {
5+
6+
var concat = function (arr) {
7+
var s = '';
8+
for (var i = 0; i < arr.length; i++) {
9+
s += arr[i];
10+
}
11+
12+
return s;
13+
};
14+
15+
var equalPerm = function (a, b) {
16+
a = (a + '').split('');
17+
b = (b + '').split('');
18+
a.sort();
19+
b.sort();
20+
return concat(a) == concat(b);
21+
};
22+
23+
var f = (num + '').split('');
24+
f.sort(function (a, b) { return a < b; });
25+
26+
if (concat(f) == num) {
27+
return -1;
28+
}
29+
30+
for (var i = num + 1; ; i++) {
31+
if (equalPerm(num, i)) {
32+
return i;
33+
}
34+
}
35+
return equalPerm(123, 321);
36+
}

0 commit comments

Comments
 (0)