-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0306-additive-number.js
More file actions
50 lines (42 loc) · 1.86 KB
/
0306-additive-number.js
File metadata and controls
50 lines (42 loc) · 1.86 KB
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
/**
* Additive Number
* Time Complexity: O(N^3)
* Space Complexity: O(N)
*/
var isAdditiveNumber = function (num) {
const totalStringLength = num.length;
if (totalStringLength < 3) {
return false;
}
const verifyAdditiveSequence = (currentScanIndex, previousNumberOne, previousNumberTwo, sequenceElementCount) => {
if (currentScanIndex === totalStringLength) {
return sequenceElementCount >= 3;
}
const calculatedSum = previousNumberOne + previousNumberTwo;
const calculatedSumString = calculatedSum.toString();
const sumStringLength = calculatedSumString.length;
if (currentScanIndex + sumStringLength > totalStringLength ||
num.substring(currentScanIndex, currentScanIndex + sumStringLength) !== calculatedSumString) {
return false;
}
return verifyAdditiveSequence(currentScanIndex + sumStringLength, previousNumberTwo, calculatedSum, sequenceElementCount + 1);
};
for (let firstEndIndex = 0; firstEndIndex < totalStringLength - 2; firstEndIndex++) {
const firstSegmentValue = num.substring(0, firstEndIndex + 1);
if (firstSegmentValue.length > 1 && firstSegmentValue[0] === '0') {
break;
}
const firstNumericValue = BigInt(firstSegmentValue);
for (let secondEndIndex = firstEndIndex + 1; secondEndIndex < totalStringLength - 1; secondEndIndex++) {
const secondSegmentValue = num.substring(firstEndIndex + 1, secondEndIndex + 1);
if (secondSegmentValue.length > 1 && secondSegmentValue[0] === '0') {
break;
}
const secondNumericValue = BigInt(secondSegmentValue);
if (verifyAdditiveSequence(secondEndIndex + 1, firstNumericValue, secondNumericValue, 2)) {
return true;
}
}
}
return false;
};