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”, “geeksfor”, “geeksforgeeks”}
Output:
geeksforgeeks
geeksfor
geeks
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.
C++
bool my_compare(string a, string b)
{
if (a.compare(0, b.size(), b) == 0
|| b.compare(0, a.size(), a) == 0)
return a.size() > b.size();
else return a < b;
}
|
Java
public static boolean my_compare(String a, String b)
{
if (a.compareTo(b) == 0 || b.compareTo(a) == 0 )
return a.length() > b.length();
else {
if (a.length() < b.length())
return true ;
}
return false ;
}
|
Python3
def my_compare(a, b):
if (a.compare( 0 , b.length, b) is 0 or b.compare( 0 , a.length, a) is 0 ):
return a.length > b.length;
else :
return a < b;
|
C#
using System;
using System.Collections.Generic;
public static bool my_compare( string a, string b)
{
if (a.CompareTo(b) == 0 || b.CompareTo(a) == 0)
return a.Length > b.Length;
else {
if (a.Length < b.Length)
return true ;
}
return false ;
}
|
Javascript
function my_compare(a,b)
{
if (a.compare(0, b.length, b) == 0
|| b.compare(0, a.length, a) == 0)
return a.length > b.length;
else
return a < b;
}
|
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Print(vector<string> v)
{
for ( auto i : v)
cout << i << endl;
}
bool my_compare(string a, string b)
{
if (a.compare(0, b.size(), b) == 0
|| b.compare(0, a.size(), a) == 0)
return a.size() > b.size();
else
return a < b;
}
int main()
{
vector<string> v = { "batman" , "bat" , "apple" };
sort(v.begin(), v.end(), my_compare);
Print(v);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static int comp(String a, int s, int e, String b)
{
if (a.length() >= e && b.length() >= e) {
for ( int i = s; i < e; i++) {
if (a.charAt(i) != b.charAt(i))
return - 1 ;
}
}
else
return - 1 ;
return 0 ;
}
public static void Print(List<String> v)
{
for (String i : v)
System.out.println(i);
}
public static void main(String[] args)
{
List<String> v = new ArrayList<String>();
v.add( "batman" );
v.add( "bat" );
v.add( "apple" );
Collections.sort(v, new Comparator<String>() {
@Override
public int compare( final String a, String b)
{
if (comp(a, 0 , b.length(), b) == 0
|| comp(b, 0 , a.length(), a) == 0 ) {
if (a.length() > b.length()) {
return 1 ;
}
else
return 1 ;
}
else {
if (a.length() < b.length())
return - 1 ;
}
return - 1 ;
}
});
Print(v);
}
}
|
Python3
def Print (v):
print (v)
def comp(a,s,e,b):
if ( len (a) > = e and len (b) > = e):
for i in range (s,e):
if (a[i] is not b[i]):
return - 1
else :
return - 1
return 0
def sort(v,lst):
a = v[ 1 ]
b = v[ 0 ]
c = v[ 2 ]
if (c<a and c<b):
lst.append(c)
if (comp(a, 0 , len (b), b) is 0 or comp(b, 0 , len (a), a) is 0 ):
if ( len (a)> len (b)):
lst.append(a)
lst.append(b)
else :
lst.append(b)
lst.append(a)
v = [ "batman" , "bat" , "apple" ]
lst = []
sort(v,lst)
Print (lst)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void Print(List< string > v)
{
for ( int i = 0; i < v.Count; i++) {
Console.WriteLine(v[i]);
}
}
public static int comp(String a, int s, int e, String b)
{
if (a.Length >= e && b.Length >= e) {
for ( int i = s; i < e; i++) {
if (a[i] != b[i])
return -1;
}
}
else
return -1;
return 0;
}
public static int compare(String a, String b)
{
if (comp(a, 0, b.Length, b) == 0
|| comp(b, 0, a.Length, a) == 0) {
if (a.Length > b.Length) {
return -1;
}
else
return 1;
}
else {
if (a.Length > b.Length)
return 1;
}
return -1;
}
static public void Main()
{
List< string > v = new List< string >();
v.Add( "batman" );
v.Add( "bat" );
v.Add( "apple" );
v.Sort(compare);
Print(v);
}
}
|
Javascript
<script>
function Print(v)
{
console.log(v);
}
function comp(a,s,e,b)
{
if (a.length >= e && b.length >= e) {
for (let i = s; i < e; i++) {
if (a[i] != b[i])
return -1;
}
}
else
return -1;
return 0;
}
function compare(a,b)
{
if (comp(a, 0, b.length, b) == 0
|| comp(b, 0, a.length, a) == 0) {
if (a.length > b.length) {
return -1;
}
else
return -1;
}
else {
if (a.length < b.length)
return 1;
}
return 1;
}
v = [ "batman" , "bat" , "apple" ];
v.sort(compare);
Print(v);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
25 Oct, 2022
Like Article
Save Article