2264. Largest 3-Same-Digit Number in String

2264. Largest 3-Same-Digit Number in String

Your given a string num. From the given string you need to find a good integer that meet the following conditions:

  1. It is a substring of num which length is 3
  2. It consists only unique digit. You have to return a maximum good integer as a string or an empty string "" if no such interger exist.

As we see the constrain is only 10^3 so we can generate all the substring and then just checked that if there three digits are same or not? if same then we will find the next big substring integer. After travaer the full string we have the answer.

Code:

string largestGoodInteger(string num) {
    vector<string>vs;
    string mx = "";
    for(int i=0; i<num.size()-2; i++){
        string s = num.substr (i,3);
        if(num[i]==num[i+1] && num[i]==num[i+2]){
            mx = max(s,mx);
        }
        vs.push_back(s);
    }
    return mx;        
}