Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions 2264.largest-3-same-digit-number-in-string.js
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 21 additions & 0 deletions largestGoodInteger.test.js
Original file line number Diff line number Diff line change
@@ -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("");
});