Greatest String

You are given a string S and an integer Q. You are allowed to perform at most Q operations on the string. In one operation, you can change any vowel to it's next character (e.g., 'a'->'b', 'e'->'f', 'i'->'j', 'o'->'p', 'u'->'v'). Generate the lexicographically greatest string by performing at most Q operations on string S.

Note- Vowels in English alphabet are- 'a','e','i','o','u'.

Example:

Input:  s = "abcde", q = 3
Output: bbcdf

Approach

C++

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

string greatestString(string sint q)
{
    int i = 0;
    int n = s.size();
    while (q && i < n)
    {
        if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' 
|| s[i] == 'o' || s[i] == 'u')
        {
            s[i] = s[i] + 1;
            q--;
        }
        i++;
    }
    return s;
}
int main()
{
    string s = "abcde";

    int q = 3;

    cout << greatestString(sq<< "\n";

    return 0;
}


No comments:

Post a Comment