From d917b30da03a641bd427c04b24b9a744bdaf4bf9 Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Mon, 16 Jan 2023 10:00:36 -0800 Subject: [PATCH 1/2] solve 2264. Largest 3-Same-Digit Number in String --- 2264.largest-3-same-digit-number-in-string.js | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 2264.largest-3-same-digit-number-in-string.js 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 From 343213ad52ab1da5b6adaf598d8b78313d172e07 Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Mon, 16 Jan 2023 10:00:56 -0800 Subject: [PATCH 2/2] test function largestGoodInteger --- largestGoodInteger.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 largestGoodInteger.test.js 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