diff --git a/2264.largest-3-same-digit-number-in-string.js b/2264.largest-3-same-digit-number-in-string.js new file mode 100644 index 00000000..43d45144 --- /dev/null +++ b/2264.largest-3-same-digit-number-in-string.js @@ -0,0 +1,26 @@ +/* URL of this problem + * https://leetcode.com/problems/largest-3-same-digit-number-in-string/description/ + * + * @param {string} num + * @return {string} + */ + +var largestGoodInteger = function(num) { + const ThreeSameDigitNums = []; + let maxGoodInt = -Infinity; + + for (let i = 0; i < num.length - 2; i++) { + if (num.charAt(i) === num.charAt(i + 1) && num.charAt(i) === num.charAt(i + 2)) { + ThreeSameDigitNums.push(num.substring(i, i + 3)); + } + } + ThreeSameDigitNums.forEach(num => { + if (maxGoodInt < num) { + maxGoodInt = num; + } + }) + + return maxGoodInt >= 0 ? maxGoodInt : ""; +}; + +module.exports = largestGoodInteger; \ No newline at end of file diff --git a/largestGoodInteger.test.js b/largestGoodInteger.test.js new file mode 100644 index 00000000..3153435d --- /dev/null +++ b/largestGoodInteger.test.js @@ -0,0 +1,21 @@ +const largestGoodInteger = require("../jest-test/2264.largest-3-same-digit-number-in-string"); + +test("Return the maximum good integer from num with multiple good integers", () => { + expect(largestGoodInteger("6777133339")).toBe("777"); +}); + +test("Return 000 if it is the only good integer", () => { + expect(largestGoodInteger("2300019")).toBe("000"); +}); + +test("Return an empty string if num has only two elements", () => { + expect(largestGoodInteger("42")).toBe(""); +}); + +test("Return an empty string if num has only one elements", () => { + expect(largestGoodInteger("4")).toBe(""); +}); + +test("Return an empty string if num has no elements", () => { + expect(largestGoodInteger("")).toBe(""); +}); \ No newline at end of file