Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
You are given a string num representing a large integer. An integer is good if it meets the following conditions:
num with length 3.Return the maximum good integer as a string or an empty string "" if no such integer exists.
Note:
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 <= 1000num only consists of digits.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"6777133339"
"2300019"
"42352338"
largest-odd-number-in-string)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2264: Largest 3-Same-Digit Number in String
class Solution {
public String largestGoodInteger(String num) {
for (int i = 9; i >= 0; i--) {
String s = String.valueOf(i).repeat(3);
if (num.contains(s)) {
return s;
}
}
return "";
}
}
// Accepted solution for LeetCode #2264: Largest 3-Same-Digit Number in String
func largestGoodInteger(num string) string {
for c := '9'; c >= '0'; c-- {
if s := strings.Repeat(string(c), 3); strings.Contains(num, s) {
return s
}
}
return ""
}
# Accepted solution for LeetCode #2264: Largest 3-Same-Digit Number in String
class Solution:
def largestGoodInteger(self, num: str) -> str:
for i in range(9, -1, -1):
if (s := str(i) * 3) in num:
return s
return ""
// Accepted solution for LeetCode #2264: Largest 3-Same-Digit Number in String
/**
* [2264] Largest 3-Same-Digit Number in String
*
* 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 = "6<u>777</u>133339"
* Output: "777"
* Explanation: There are two distinct good integers: "777" and "333".
* "777" is the largest, so we return "777".
*
* Example 2:
*
* Input: num = "23<u>000</u>19"
* 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.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/largest-3-same-digit-number-in-string/
// discuss: https://leetcode.com/problems/largest-3-same-digit-number-in-string/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn largest_good_integer(num: String) -> String {
num.as_bytes()
.windows(3)
.filter(|bytes| bytes.iter().all(|&b| b == bytes[0])) //[[55,55,55]], [57,57,57]]
.map(|b| b[0])
.max()
.map(|b| (b as char).to_string().repeat(3))
.unwrap_or_default() //"999"
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2264_example_1() {
let num = "6777133339".to_string();
let result = "777".to_string();
assert_eq!(Solution::largest_good_integer(num), result);
}
#[test]
fn test_2264_example_2() {
let num = "2300019".to_string();
let result = "000".to_string();
assert_eq!(Solution::largest_good_integer(num), result);
}
#[test]
fn test_2264_example_3() {
let num = "42352338".to_string();
let result = "".to_string();
assert_eq!(Solution::largest_good_integer(num), result);
}
}
// Accepted solution for LeetCode #2264: Largest 3-Same-Digit Number in String
function largestGoodInteger(num: string): string {
for (let i = 9; i >= 0; i--) {
const s = String(i).repeat(3);
if (num.includes(s)) {
return s;
}
}
return '';
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.