-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsolution.js
More file actions
48 lines (38 loc) · 1.26 KB
/
solution.js
File metadata and controls
48 lines (38 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* @param {string} num
* @return {string}
*/
var largestGoodInteger = function(num) {
let result = "";
// Check for 3 consecutive same digits starting from the left
for (let i = 0; i <= num.length - 3; i++) {
// Check if current position and next two positions have the same digit
if (num[i] === num[i + 1] && num[i + 1] === num[i + 2]) {
const current = num.substring(i, i + 3);
// Update result if current is larger (lexicographically)
if (current > result) {
result = current;
}
}
}
return result;
};
// Alternative approach using sliding window
var largestGoodInteger2 = function(num) {
let result = "";
// Use sliding window of size 3
for (let i = 0; i <= num.length - 3; i++) {
const c1 = num[i];
const c2 = num[i + 1];
const c3 = num[i + 2];
// Check if all three characters are the same
if (c1 === c2 && c2 === c3) {
const current = c1 + c2 + c3;
// Update result if current is larger
if (result === "" || current > result) {
result = current;
}
}
}
return result;
};