-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
56 lines (45 loc) · 1.2 KB
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var globalUniqueIntStorage = [];
function recursiveIntGenerator(arrayInt, depth, arrangement) {
for (var i = 0; i < arrayInt.length; i++) {
if (arrangement.indexOf(i) !== -1) {
continue;
}
var newArrangement = arrangement.slice();
newArrangement.push(i);
if (depth + 1 == arrayInt.length - 1) {
var combined = '';
for (var j = 0; j < newArrangement.length; j++) {
combined += arrayInt[newArrangement[j]];
}
if (globalUniqueIntStorage.indexOf(combined) === -1) {
globalUniqueIntStorage.push(combined);
}
} else {
recursiveIntGenerator(arrayInt, depth + 1, newArrangement);
}
}
}
function occupyStorage(arrayInt) {
for (var i = 0; i < arrayInt.length; i++) {
if (arrayInt[i] == '0') {
continue;
}
recursiveIntGenerator(arrayInt, 0, [i]);
}
}
function solution(A) {
globalUniqueIntStorage = [];
var strN = parseInt(A).toString();
var arrayN = strN.split('');
if (parseInt(A) < 0) {
throw new Error('Invalid value. Accepts only non-negative integers only.');
}
if (strN.length == 1) {
return 1;
}
if (strN.length > 1 && arrayN[0] == '0') {
throw new Error('Invalid integer.');
}
occupyStorage(arrayN);
return globalUniqueIntStorage.length;
}