Number of Different Integers in a String LeetCode solution in JavaScript

1
const CC0 = "0".charCodeAt(0);
2

3
var numDifferentIntegers = function (word) {
4
const numStrSet = new Set();
5

6
// get numbers as strings
7
const numStrs = word.split(/[^0-9]+/);
8

9
// drop leading zeros
10
for (const numStr of numStrs) {
11
if (numStr.length > 0) {
12
let i = 0;
13
while (numStr.charCodeAt(i) === CC0) i++;
14

15
// make sure that we preserve last 0 if string is composed of zeros only
16
numStrSet.add(numStr.slice(i) || "0");
17
}
18
}
19

20
return numStrSet.size;
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0