Please look into compare function, it does not work
Hey @krishav49
Your compare function is incorrect
Output for this
2
appp
ap
should be
appp
ap
Here refer to correct compare here
int compareTo(string s1, string s2) {
int i = 0;
while (i < s1.length() && i < s2.length()) {
if (s1[i] > s2[i]) {
return 1;
} else if (s1[i] < s2[i]) {
return -1;
}
i++;
}
if (s1.length() > s2.length()) {
return -1;
} else {
return 1;
}
}
It’s not working…
Hey @krishav49
Sorry for that ,check this one
bool compare(string a, string b){
if (a.find(b)==0 || b.find(a)==0){ //If a was prefix of b or b is prefix of a
return a.length() > b.length(); //then we place the one with greater length before other
}
return a < b; //else we place by dictionary order
}
if (a.find(b)==0 || b.find(a)==0)
I don’t understand how it is work, please explain it briefly
string.find(substring) // find finds the first occurence of substringi in string
so
if b is prefix of a then a.find(b) will return 0 because present at 0 index
similarly b.find(a)
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.