i have been trying to solve this question but not been able to solve it .Can anyone please give an approach to solve this.I think it could be solved by subset sum with fixed subset size but can anyone provide me their insight??

Input the string characterwise to find the hash.

You can use

Hash+=arr[i] -‘a’

Put as many z as possible at the end and then put whatevers left. If the S is the same as what we got flip the last character change i.e

aaaaagzzzzzz becomes

aaaaazgzzzzz

1 Like

thanks

!you saved my sleep

but in the case of a string like abz the flipping part wont work i guess

Azb?

no the lexicographically smaller string is acy

Abz is smaller.

Lexographic is just alphabetical

Wait you’re right

1 Like

greedy solutions are not working that is the main problem

It’s not flipping +1 to the least non z character and -1 to z making it y. My fault.