Sort strings prob

https://online.codingblocks.com/player/10922/content/4738?s=1615 or https://hack.codingblocks.com/contests/c/262/103
code-https://ide.codingblocks.com/s/40963

I am getting stuck, have put in my doubts along with the code

pls help, thanks

You need to print longer before only when one is substring of other.
So, sort alphabetically in general, check for substring between two consecutive, if found substring then display the longer one before shorter one

yes, I tried ORing and ANDing both the conditions(if that’s what you mean to say), but it didn’t work
Basically check_substring func in my code shouldn’t work for 2 diff strings, even if it gets entered, but somehow diff strings are also getting compared and returning an answer, and that’s what messes the o/p

No there is no need of ORing and ANDing both the conditions

Simple approach to this question
First sort simply, which would give you lexicographically (dictionary ordered) array.
This would be containing shorter substring before the longer string.

So now compare consecutive elements and if there is substring of other, then swap them.

Hope this helps and solves your problem :relaxed:

There are three conditions here:

  1. First string is substring of second string
  2. Second string is substring of first string
  3. Neither string is substring of another string

Condition should be such that:

  • In first two cases, length of first should be greater than second.
  • In third case, first string should be lexicographically smaller than second string.

Tip for conditions in comparator:
Condition should be such that it return true in cases when objects should NOT swap.

In our case, conditions will be a.length() > b.length() or a < b

Happy coding :slight_smile:

https://ide.codingblocks.com/s/41648

passed test cases, but is this approach fine, is there a better one?

Yes, it is correct and optimized. :love_you_gesture::+1:

Here’s my solution : https://ide.codingblocks.com/s/43046

1 Like

Correct , but when strings are same , how can i campare consecutive, i think given test cases in question is very simple.