if the i/p is
abhay
aaliya
anjali
then o/p
should be
aaliya
anjali
abhay
as the prefix are same then length should be compare
plz tell
and do some changes in my code
if the i/p is
abhay
aaliya
anjali
then o/p
should be
aaliya
anjali
abhay
as the prefix are same then length should be compare
plz tell
and do some changes in my code
first of all. the expected output for your example is:aaliya abhay anjali…
“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.”
length are not compared when only first character matches but if a string is present completely as a prefix in another string, then you compare length.
the question says that you need to sort string in way it should be stored in dictionary. but there are few special cases like (ans, answer), which should come first? answer should come first.
ans is completely present in answer, so answer comes first as its length is larger.
problem in your code:
in compare() function, you have only checked the first character. this is the problem. check for whole string like you usually do to sort strings. compare lengths only when that particular case arise.
another example could be: cat dogs catalog an animal
output: animal an catalog cat dogs
thanks
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.