Mike and LMS

Disappointed and Disheartened Mike is really tired of jail life and needs to take his mind off all this. Mike's roommate and good friend Kevin decides to help him, and tells him to try the following task:

Given a string S consisting of lowercase English alphabets of size N, can you find the lexicographically maximum substring of this string? Mike has no knowledge whatsoever of strings and needs your help. Can you help Mike and improve his mood?

Example:

Input:  n = 6, s = "ababaa"
Output: babaa

Approach

C++

#include <bits/stdc++.h>
using namespace std;

string mikeAndLMS(string sint n)
{
    char maxi = 0;

    set<stringgreater<string>> p;
    for (int i = 0i < s.size(); i++)
    {
        p.insert(s.substr(i));
    }
    return *(p.begin());
}
int main()
{
    int n = 6;
    string s = "ababaa";

    cout << mikeAndLMS(sn);

    return 0;
}


No comments:

Post a Comment