LeetCode 2264: Largest 3-Same-Digit Number in String Solution

Master LeetCode problem 2264 (Largest 3-Same-Digit Number in String), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2264. Largest 3-Same-Digit Number in String

Easy

Problem Explanation

Explanation:

To solve this problem, we can iterate through all substrings of length 3 in the input string and check if each substring consists of only one unique digit. If it does, we compare it with the current maximum good integer found so far and update it if the current substring is larger.

Algorithm:

  1. Initialize a variable maxGood as an empty string to store the maximum good integer found.
  2. Iterate through all substrings of length 3 in the input string.
  3. For each substring, check if it consists of only one unique digit.
  4. If it does, compare it with maxGood and update maxGood if the current substring is larger.
  5. Return maxGood as the result.

Time Complexity:

  • The time complexity of this algorithm is O(n), where n is the length of the input string num.

Space Complexity:

  • The space complexity of this algorithm is O(1) as we are using a constant amount of extra space.

:

Solution Code

class Solution {
    public String findGoodNumber(String num) {
        String maxGood = "";
        
        for (int i = 0; i <= num.length() - 3; i++) {
            String current = num.substring(i, i + 3);
            if (current.charAt(0) == current.charAt(1) && current.charAt(1) == current.charAt(2)) {
                if (current.compareTo(maxGood) > 0) {
                    maxGood = current;
                }
            }
        }
        
        return maxGood;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2264 (Largest 3-Same-Digit Number in String)?

This page provides optimized solutions for LeetCode problem 2264 (Largest 3-Same-Digit Number in String) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2264 (Largest 3-Same-Digit Number in String)?

The time complexity for LeetCode 2264 (Largest 3-Same-Digit Number in String) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2264 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2264 in Java, C++, or Python.

Back to LeetCode Solutions