Skip to content

Commit e814735

Browse files
Add solution for Largest 3-Same-Digit Number in String (LeetCode 2264)
1 parent 06a6fd0 commit e814735

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Largest 3-Same-Digit Number in String
2+
3+
## Problem Description
4+
Given a string `num` representing a large integer, return the largest **good integer** as a string. A **good integer** is a substring of `num` with exactly 3 consecutive digits that are the same.
5+
6+
If no such integer exists, return an empty string `""`.
7+
8+
## Approach
9+
10+
### Method 1: Simple Iteration
11+
The most straightforward approach is to iterate through the string and check for 3 consecutive same digits.
12+
13+
**Algorithm:**
14+
1. Initialize an empty result string
15+
2. Iterate through the string from index 0 to `length - 3`
16+
3. For each position, check if the current digit and next two digits are the same
17+
4. If found, extract the substring and update result if it's larger lexicographically
18+
5. Return the result
19+
20+
**Time Complexity:** O(n) - where n is the length of the string
21+
**Space Complexity:** O(1) - constant extra space
22+
23+
### Method 2: Sliding Window
24+
A more explicit approach using a sliding window of size 3.
25+
26+
**Algorithm:**
27+
1. Initialize an empty result string
28+
2. Use a sliding window of size 3 to check consecutive digits
29+
3. Compare all three characters in the window
30+
4. If they're the same, form the substring and update result if larger
31+
5. Return the result
32+
33+
**Time Complexity:** O(n) - where n is the length of the string
34+
**Space Complexity:** O(1) - constant extra space
35+
36+
## Key Insights
37+
1. **Consecutive Check**: We need exactly 3 consecutive same digits, not just 3 occurrences
38+
2. **Lexicographical Order**: String comparison works naturally for digits (e.g., "999" > "888")
39+
3. **Empty String**: Return empty string if no good integer is found
40+
4. **Boundary Check**: Only iterate up to `length - 3` to avoid index out of bounds
41+
42+
## Examples
43+
- **num = "6777133339"**:
44+
- Found "333" at position 5
45+
- Found "777" at position 1
46+
- "777" > "333" lexicographically, so return "777"
47+
- **num = "2300019"**:
48+
- Found "000" at position 2
49+
- Only one good integer, so return "000"
50+
- **num = "42352338"**:
51+
- No 3 consecutive same digits found
52+
- Return ""
53+
54+
## Edge Cases
55+
1. **String length < 3**: Return empty string
56+
2. **No good integers**: Return empty string
57+
3. **Multiple good integers**: Return the lexicographically largest
58+
4. **All same digits**: Return the first 3 digits
59+
60+
## Code Structure
61+
Both solutions are clean and efficient:
62+
1. **Simple Iteration**: More straightforward and easier to understand
63+
2. **Sliding Window**: More explicit about the window concept
64+
65+
The sliding window approach makes the logic more clear and can be easily extended to other similar problems that require checking consecutive elements.
66+
67+
## Performance Notes
68+
- Both approaches have the same time and space complexity
69+
- String comparison is efficient for small strings
70+
- The solution handles all edge cases correctly
71+
- No need for complex data structures or algorithms
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
public String largestGoodInteger(String num) {
3+
String result = "";
4+
5+
// Check for 3 consecutive same digits starting from the left
6+
for (int i = 0; i <= num.length() - 3; i++) {
7+
// Check if current position and next two positions have the same digit
8+
if (num.charAt(i) == num.charAt(i + 1) &&
9+
num.charAt(i + 1) == num.charAt(i + 2)) {
10+
11+
String current = num.substring(i, i + 3);
12+
13+
// Update result if current is larger (lexicographically)
14+
if (current.compareTo(result) > 0) {
15+
result = current;
16+
}
17+
}
18+
}
19+
20+
return result;
21+
}
22+
}
23+
24+
// Alternative approach using sliding window
25+
class Solution2 {
26+
public String largestGoodInteger(String num) {
27+
String result = "";
28+
29+
// Use sliding window of size 3
30+
for (int i = 0; i <= num.length() - 3; i++) {
31+
char c1 = num.charAt(i);
32+
char c2 = num.charAt(i + 1);
33+
char c3 = num.charAt(i + 2);
34+
35+
// Check if all three characters are the same
36+
if (c1 == c2 && c2 == c3) {
37+
String current = String.valueOf(c1) + c2 + c3;
38+
39+
// Update result if current is larger
40+
if (result.isEmpty() || current.compareTo(result) > 0) {
41+
result = current;
42+
}
43+
}
44+
}
45+
46+
return result;
47+
}
48+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @param {string} num
3+
* @return {string}
4+
*/
5+
var largestGoodInteger = function(num) {
6+
let result = "";
7+
8+
// Check for 3 consecutive same digits starting from the left
9+
for (let i = 0; i <= num.length - 3; i++) {
10+
// Check if current position and next two positions have the same digit
11+
if (num[i] === num[i + 1] && num[i + 1] === num[i + 2]) {
12+
13+
const current = num.substring(i, i + 3);
14+
15+
// Update result if current is larger (lexicographically)
16+
if (current > result) {
17+
result = current;
18+
}
19+
}
20+
}
21+
22+
return result;
23+
};
24+
25+
// Alternative approach using sliding window
26+
var largestGoodInteger2 = function(num) {
27+
let result = "";
28+
29+
// Use sliding window of size 3
30+
for (let i = 0; i <= num.length - 3; i++) {
31+
const c1 = num[i];
32+
const c2 = num[i + 1];
33+
const c3 = num[i + 2];
34+
35+
// Check if all three characters are the same
36+
if (c1 === c2 && c2 === c3) {
37+
const current = c1 + c2 + c3;
38+
39+
// Update result if current is larger
40+
if (result === "" || current > result) {
41+
result = current;
42+
}
43+
}
44+
}
45+
46+
return result;
47+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution:
2+
def largestGoodInteger(self, num: str) -> str:
3+
result = ""
4+
5+
# Check for 3 consecutive same digits starting from the left
6+
for i in range(len(num) - 2):
7+
# Check if current position and next two positions have the same digit
8+
if num[i] == num[i + 1] == num[i + 2]:
9+
10+
current = num[i:i + 3]
11+
12+
# Update result if current is larger (lexicographically)
13+
if current > result:
14+
result = current
15+
16+
return result
17+
18+
# Alternative approach using sliding window
19+
class Solution2:
20+
def largestGoodInteger(self, num: str) -> str:
21+
result = ""
22+
23+
# Use sliding window of size 3
24+
for i in range(len(num) - 2):
25+
c1, c2, c3 = num[i], num[i + 1], num[i + 2]
26+
27+
# Check if all three characters are the same
28+
if c1 == c2 == c3:
29+
current = c1 + c2 + c3
30+
31+
# Update result if current is larger
32+
if not result or current > result:
33+
result = current
34+
35+
return result

0 commit comments

Comments
 (0)