What is the error in this ? It is passing the given sample test case

#include
#include
#include
using namespace std;

bool isSubstring(string a,string b,int l){
string r=b.substr(0,l);
if(r==a){
return true;
}
else{
return false;
}
}
int main() {
string s[10];
int n;
cin>>n;
for(int i=0;i<n; i++)
cin>>s[i];

sort(s, s+n);
for(int i=0;i<n-1;i++){
	string a=s[i];
	string b=s[i+1];
	int l=a.length();
	if(isSubstring(a,b,l)){
		swap(s[i],s[i+1]);
	}
}
for(int i=0;i<n; i++){
	cout<<s[i]<<" ";
}
return 0;

}

I have first sorted simply, which would give a lexicographically array.

And then I have compared consecutive elements and if one is a substring of the other then I have swapped them.
Please let me know what is wrong with the approach/implementation .

hello @eesha

check now->

1 Like

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.