[LeetCode] 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.
1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
string largestGoodInteger(string num) {
string res = "";
for(int i = 0; i < num.length() - 2; i++) {
if(num[i] == num[i+1] and num[i+1] == num[i+2]) {
res = max(res, string(3,num[i]));
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/12/04/PS/LeetCode/largest-3-same-digit-number-in-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.