Leetcode 2264: Largest 3-Same-Digit Number in String

grid47
grid47
Exploring patterns and algorithms
Mar 25, 2024 5 min read

You are given a string num representing a large integer. A ‘good’ integer is defined as a substring of length 3, consisting of only one unique digit. Your task is to find the largest ‘good’ integer in the string. If no such integer exists, return an empty string.
Problem
Approach
Steps
Complexity
Input: You are given a string `num` representing a large integer.
Example: num = "98711111234"
Constraints:
• 3 <= num.length <= 1000
• num only consists of digits.
Output: Return the largest good integer as a string. If no such integer exists, return an empty string.
Example: Output: "111"
Constraints:
Goal: The goal is to find the maximum substring of length 3 that consists of only one unique digit.
Steps:
• Iterate through the string starting from index 2 to check all possible substrings of length 3.
• For each substring, check if all characters are the same.
• If a valid substring is found, compare it with the current maximum substring to keep the largest.
• Return the largest valid substring or an empty string if no such substring is found.
Goal: The input string `num` has a length between 3 and 1000, consisting only of digits.
Steps:
• The string `num` will always have a length of at least 3.
• The string only contains digits.
Assumptions:
• The string `num` will not be empty, as its length is always at least 3.
Input: num = "98711111234"
Explanation: In this case, the valid good integers are '111'. The largest good integer is '111', so the output is '111'.

Input: num = "223335"
Explanation: Here, the good integers are '333' and '222'. The largest good integer is '333', so the output is '333'.

Input: num = "12345"
Explanation: There are no substrings of length 3 that consist of only one unique digit. Therefore, the output is an empty string.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus