Skip to content

Commit

Permalink
problem: new problem solution - 2264 . Largest 3-Same-Digit Number in…
Browse files Browse the repository at this point in the history
… String
  • Loading branch information
squxq committed Dec 4, 2023
1 parent 8c4ceac commit 95898f2
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ My approach to solving LeetCode problems typically involves the following steps:
| 1662 | [Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/) | Algorithms | [TypeScript](./problems/algorithms/checkIfTwoStringArraysAreEquivalent/CheckIfTwoStringArraysAreEquivalent.ts) | Easy |
| 1160 | [Find Words That Can Be Formed by Characters](https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/) | Algorithms | [TypeScript](./problems/algorithms/findWordsThatCanBeFormedByCharacters/FindWordsThatCanBeFormedByCharacters.ts) | Easy |
| 1266 | [Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/) | Algorithms | [TypeScript](./problems/algorithms/minimumTimeVisitingAllPoints/MinimumTimeVisitingAllPoints.ts) | Easy |
| 2264 | [Largest 3-Same-Digit Number in String](https://leetcode.com/problems/largest-3-same-digit-number-in-string/) | Algorithms | [TypeScript](./problems/algorithms/largest3SameDigitNumberInString/Largest3SameDigitNumberInString.ts) | Easy |
| ... | ... | ... | ... | ... |

In this table:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Source : https://leetcode.com/problems/largest-3-same-digit-number-in-string/
// Author : Francisco Tomas
// Date : 2023-12-04

import { largestGoodInteger } from "./Largest3SameDigitNumberInString";

describe("largest 3-same-digit number in string", () => {
test("example 1", () => {
const result: string = largestGoodInteger("6777133339");
expect(result).toBe("777");
});

test("example 2", () => {
const result: string = largestGoodInteger("2300019");
expect(result).toBe("000");
});

test("example 3", () => {
const result: string = largestGoodInteger("42352338");
expect(result).toBe("");
});

test("complext example - example 4", () => {
const result: string = largestGoodInteger("3200014888");
expect(result).toBe("888");
});

test("another complex example - example 5", () => {
const result: string = largestGoodInteger("1134324999");
expect(result).toBe("999");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Source : https://leetcode.com/problems/largest-3-same-digit-number-in-string/
// Author : Francisco Tomas
// Date : 2023-12-04

/*****************************************************************************************************
*
* You are given a string num representing a large integer. An integer is good if it meets the
* following conditions:
*
* It is a substring of num with length 3.
* It consists of only one unique digit.
*
* Return the maximum good integer as a string or an empty string "" if no such integer exists.
*
* Note:
*
* A substring is a contiguous sequence of characters within a string.
* There may be leading zeroes in num or a good integer.
*
* Example 1:
*
* Input: num = "6777133339"
* Output: "777"
* Explanation: There are two distinct good integers: "777" and "333".
* "777" is the largest, so we return "777".
*
* Example 2:
*
* Input: num = "2300019"
* Output: "000"
* Explanation: "000" is the only good integer.
*
* Example 3:
*
* Input: num = "42352338"
* Output: ""
* Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no
* good integers.
*
* Constraints:
*
* 3 <= num.length <= 1000
* num only consists of digits.
******************************************************************************************************/

/**
* string -> string
* return the maximum good integer in the given string, num,
* as a string or an empty string, "", if no such integer exists
* NOTE: an integer is good if it meets the following conditions:
* - it is a substring of num with length 3;
* - it consists of only one unique digit.
* Stub:
function largestGoodInteger(num: string): string {return ""}
* Tests:
* I: num = "6777133339" -> O: "777"
* I: num = "2300019" -> O: "000"
* I: num = "42352338" -> O: ""
* I: num = "3200014888" -> O: "888"
* I: num = "1134324999" -> O: "999"
* Template:
function largestGoodInteger(num: string): string {
return (... num)
}
*/

/**
* Time Complexity: O(n), where n is the length of num
* Space Complexity: O(1)
* Runtime: 53ms (95.45%)
* Memory: 44.51MB (63.64%)
*/
export function largestGoodIntegerV1(num: string): string {
let ans: string = "";
let j: number = 0;

for (let i: number = 1; i < num.length; i++) {
if (num[j] === num[i]) {
if (i - j === 2) {
const goodInt: string = num.slice(j, i + 1);
if (ans === "" || +ans < +goodInt) {
ans = goodInt;
}
}
} else {
j += i - j;
}
}

return ans;
}

/**
* Time Complexity: O(n), where n is the length of num
* Space Complexity: O(1)
* Runtime: 49ms (95.45%)
* Memory: 43.72MB (100.00%)
*/
export function largestGoodInteger(num: string): string {
let maxGoodInt: string = "\0";

for (let i: number = 2; i < num.length; i++) {
if (num[i] === num[i - 1] && num[i] === num[i - 2]) {
maxGoodInt = String.fromCharCode(
Math.max(maxGoodInt.charCodeAt(0), num.charCodeAt(i)),
);
}
}

return maxGoodInt === "\0" ? "" : maxGoodInt.repeat(3);
}

0 comments on commit 95898f2

Please sign in to comment.