Https://ide.codingblocks.com/s/164689

it is sorting in lexicographical order but the longer length is not being returned first,instead the shorter one is being returned.Please help.

@srshubhi398 For this question you can make an array of strings and then sort it according to the condition that if there is a prefix match between two string, sort in decreasing order of length. Use C++ inbuilt sort function with a custom comparator something like this:

bool mycompare( string a, string b)
{
int l1=a.length();int l2=b.length();int flag=-1;
for(int i=0;i<min(l1,l2);i++){
if(a[i]!=b[i])
flag=0;
}
if(flag==-1) // means there is a prefix match
return l1>l2; // so sort decreasing order of length
else
return a<b; //sorting in normal dictionary order when no prefix match
}

Hope this helps :slightly_smiling_face:

but I am not able to understand the problem with my code

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.