Largest 3-Same-Digit Number in String
In this problem, you need to find substrings of length 3 in the given string, where all the characters are the same. The goal is to find the maximum such substring in terms of numerical value.
Here’s a Python solution using a sliding window approach. We will iterate over the string with a window of size 3 and check if all characters in the window are the same. If they are, we update our answer with the maximum of the current and previous maximums.
|
|
In the code above:
n
is the length of the input stringnum
.res
is a string variable initialized as an empty string to store the maximum good integer found so far.- We use a for loop to iterate from the start of
num
to the third last character. For each indexi
, we take a slice ofnum
fromi
toi + 3
and check if all the characters in the slice are the same. If they are, we updateres
as the maximum ofres
and the current slice. - Finally, we return
res
, which is the largest good integer found innum
. If no good integer is found,res
would remain an empty string, and hence, we would return `"".