samedi 4 mai 2019

Converting a given string to contain only same characters

Suppose there is a string with only lowercase letters, what is the minimum number of steps required to convert it to another string so that the resultant string has all characters to be the same.

For ex, 'ab' has to be converted to 'aa' or 'bb'.

At each step, the character can only be manipulated as c+1 or c-1. [ Except for 'a' and 'z' i.e alphabets aren't to be considered circular]

This is the code I have so far but it gives me TLE on large inputs. In the code, I am trying to find the character which can be obtained from other characters through minimum changes by finding the difference of their ASCII values.

int Solution::solve(string A) {

    vector<int> V(A.length());
    std::fill(V.begin(),V.end(),0);

    for(long long int i = 0; i<A.length(); ++i)
    {
        for(long long int j=0;j<A.length();++j)
        {
            V[i]+=abs((int)A[i]-(int)A[j]);
        }
    }

    int a = *min_element(V.begin(),V.end());

    return a;


}

I would appreciate any help on how to further optimize this code to avoid TLE.

Aucun commentaire:

Enregistrer un commentaire