getting run error in one of the test case
String sort-run error
when one string is completely inside another string as prefix, then only length rule applies. like in case of (bat, batman), (cat, cater)… not in cases like (cat,clown)
update required:
bool compare(string a,string b){
int n=min(a.length(),b.length());
for(int i=0;i<n;i++){
if(a[i]!=b[i]){
return a[i]<b[i];
}
}
return a.length() > b.length();
}
please rate and resolve if satisfied.
thanks
@gauravshukla789 i am unable to get what are you saying about length rule.pls elaborate it more…
and coming to your compare function my compare function is doing exactly the same work.
(and still it is showing RUN ERROR in test case after using your code also)
bool compare(string a,string b){
if(a[0]==b[0]){
int n=min(a.length(),b.length());
for(int i=1;i<n;i++){
if(a[i]!=b[i]){
return a<b;
}
}
return a.length() > b.length();
}
return a<b;
}
really sorry about that. you are right, both compare functions are exactly the same.
the issue in your code is string array declaration. that is string arr[100];
as per constraints there can be 1000 strings in input.
so either declare it of size 1000, or you can also create dynamic array of size n to save memory.
like cin>>n; string arr[n]; // this is always recommended.
thanks
yup thanks using dynamic array it is working fine
please rate and resolve,
thanks