sir if one string is present as a prefix in another string then there first letter will also be same so i just compare there first character then done sorting but only 1 testcase is passing
please tell the erroe
#include<bits/stdc++.h>
using namespace std;
bool cmp(string s1,string s2)
{ int i=0;
if(s1[i]==s2[i])
{
return s1.length()>s2.length();
}
return s1<s2;
}
int main()
{
int n;
string s[1000];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>s[i];
}
sort(s,s+n,cmp);
for(int i=0;i<n;i++)
{
cout<<s[i]<<endl;
}
return 0;
}
String sort(2 test case in not passed)
you should only compare using length when one string is prefix of the other, you are just comparing the first element which is wrong
sir if one string is present as a prefix in another string then there first letter will also be same so why should compare the whole string??
bzt, batman, first letter is the same but first string is not the prefix of the other
okk sir,got it thank u