You are given a string S of lowercase English letters with length N. You are allowed to (but don't have to) choose one index in this string and change the letter at this index to any other lowercase English letter. The cost of this operation is the absolute value of the difference of ASCII values of the new letter and the original letter; let's denote it by X
Next, consider the number of pairs of indices (i,j)
in the resulting string (the string after changing one letter, or the original string if no letter was changed) such that 1≤i<j≤N and Si<Sj. Let's denote it by Y
Find the minimum possible value of X+Y?
abcd answer is 6
dbca answer is 1
dcba answer is 0
as in dcba there is no pair (i,j) Si<Sj so answer is 0.
What I have tried:
i tried to change the minimum element to maximum and counted the number of pairs and also tried to changed maximum to minimum and counted pairs and printed min of all three.