How to solve this question

sir can this question be solved using inbuilt sort function

No, it can’t be solved directly using inbuilt sort function since for two strings where in one string is a prefix of another, inbuilt function would place the string with shorter length first and longer length after it while we want the opposite in our result. So, try building your own custom comparator.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.