Given a string s
representing a number, you need to find the largest three-same-digit number in the given string. A three-same-digit number is a sequence of the same digit appearing three times consecutively. Return that largest number as a string. If there is no such number, return an empty string.
s
?Clarification:
s
can be up to 10^5
.s
will only contain numeric digits (0-9
).#include <iostream>
#include <string>
using namespace std;
string largestThreeSameDigitNumber(string s) {
// Initialize the result as an empty string
string result = "";
int n = s.length();
for (int i = 0; i < n - 2; i++) {
if (s[i] == s[i+1] && s[i] == s[i+2]) {
string current = s.substr(i, 3);
if (result == "" || current > result) {
result = current;
}
}
}
return result;
}
int main() {
// Test cases
cout << largestThreeSameDigitNumber("6777133339") << endl; // Output: "777"
cout << largestThreeSameDigitNumber("2300019") << endl; // Output: "000"
cout << largestThreeSameDigitNumber("42352338") << endl; // Output: ""
return 0;
}
Feel free to ask for further clarifications or additional test cases!
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?