in the videos above, sorting of integers is only explained. please tell about the steps or method of solving this problem
Dont know how to take string input
Take string input using .next() like for int we use .nextInt()
you need to write a sort function for that. If the string lengths are same compare then char by char.
Don’t forget: if a string is present completely as a prefix in another string, then string with longer length should come first. Eg bat, batman are 2 strings and the string bat is present as a prefix in Batman - then sorted order should have - Batman, bat.
how to check the last condition that you mentioned
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;
}
This is a pseudo code of how your comparator should look like. Now the final condition is taken care of automatically. If length of s1> length of s2 then return -1. Swap the strings only when returned value is greater than 0.
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.