i know 2 methods to check substring in string . indexOf() and contains() but cannot implement them in the context . kindly help and make me understant the concepts.
Share the right approach
You have implemented the normal string comparator, that isn’t supposed to work for this question. What you need to do is, first check if one of the string is the prefix of another, if so is the case, then use the longer string, else use your comparator for comparing the two strings.
marked comment change . i have used startsWith() method in if condition than compareTo() , but unbale to get correct output.
It’s still wrong. don’t use any other built in method. Just calculate the minimum length out of the two strings that you are comparing, and take the substring of that length from the starting from both of the strings. Then if these 2 obtained strings are equal that means one of them is prefix of another and use the longer string else use compare function. simple as that.
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.