# Sort an array of strings lexicographically based on prefix

Given an array of strings arr[] of size N, the task is to sort the array of strings in lexicographical order and if while sorting for any two string A and string B, if string A is prefix of string B then string B should come in the sorted order.

Examples:

Input: arr[] = {“sun”, “moon”, “mock”}
Output:
mock
moon
sun
Explanation:
The lexicographical sorting is mock, moon, and sun.

Input: arr[] = {“geeks”, “geeksforgeeks”, “geeksforgeeks”}
Output:
geeksforgeeks
geeksfor
geeks

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to sort the given array of strings using the inbuilt sort function using the below comparator function. The comparator function used to check if any string occurs as a substring in another string using compare() function in C++ then, it should arrange them in decreasing order of their length.

 `bool` `my_compare(string a, string b) ` `{ ` `    ``// If any string is a substring then ` `    ``// return the size with greater length ` `    ``if` `(a.compare(0, b.size(), b) == 0 ` `        ``|| b.compare(0, a.size(), a) == 0) ` `        ``return` `a.size() & gt; ` `    ``b.size(); ` ` `  `    ``// Else return lexicographically ` `    ``// smallest string ` `    ``else` `return` `a & lt; ` `    ``b; ` `}`

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to print the vector ` `void` `Print(vector v) ` `{ ` `    ``for` `(``auto` `i : v) ` `        ``cout << i << endl; ` `} ` ` `  `// Comparator function to sort the ` `// array of string wrt given conditions ` `bool` `my_compare(string a, string b) ` `{ ` `    ``// Check if a string is present as ` `    ``// prefix in another string, then ` `    ``// compare the size of the string ` `    ``// and return the larger size ` `    ``if` `(a.compare(0, b.size(), b) == 0 ` `        ``|| b.compare(0, a.size(), a) == 0) ` ` `  `        ``return` `a.size() > b.size(); ` ` `  `    ``// Else return lexicographically ` `    ``// smallest string ` `    ``else` `        ``return` `a < b; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// GIven vector of strings ` `    ``vector v = { ``"batman"``, ``"bat"``, ``"apple"` `}; ` ` `  `    ``// Calling Sort STL with my_compare ` `    ``// function passed as third parameter ` `    ``sort(v.begin(), v.end(), my_compare); ` ` `  `    ``// Function call to print the vector ` `    ``Print(v); ` `    ``return` `0; ` `}`

Output:

```apple
batman
bat
```

Time Complexity: O(N*log N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Eat Sleep Code Repeat

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.