Sort an array of strings lexicographically based on prefix

Last Updated :

Given n strings in any order. Sort all the strings (lexicographically), but if a string is present completely as a prefix in another string, then string with larger length should come first.

Examples:

Input :
3
cap
apple
captain
 
Output :
apple
captain
cap 

Explanation:
cap is present in captain as a prefix, 
so string : captain being larger in length then cap, 
so captain will come before cap.

Approach:
Using sort STL function with a bool comparator to apply for given condition.
If a string is present as a prefix in another string, then larger string should be printed first.
Find function to check for occurrences of one string in another.
Using npos : is a static member constant value with the greatest possible value for an element of type size_t.

Implementation in C++:

#include <iostream>
#include <algorithm>
#include <cstring>
#define ull unsigned long long int
using namespace std;

bool mycompare(string a, string b)
{
    // Checking if either of string
    // a or b is prefix of another
    if ((a.find(b) != string::npos) || (b.find(a) != string::npos))
    {
        // If present, returning that string
        // with larger length
        return a.length() > b.length();
    }

    return a < b;
}

int main()
{
    int n = 3;
    string str[] = { "bat", "ball", "batsman" };

    // STL function for string sorting
    // with custom comparator (mycompare)
    sort(str, str + n, mycompare);

    for (int i = 0; i < n; i++)
        cout << str[i] << endl;

    return 0;
}

Output:

ball
batsman
bat
Comment
Next Article
Mithlesh Upadhyay Published 26 Sep, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • Find the Nth Triacontakaihenagonal Number

    Given a number N, the task is to find Nth triacontakaihenagonal number. A triacontakaihenagonal number is class of figurate number. It has 31 – sided polygon called triacontakaihenagon.…

    1 min read
  • Check if a Number is a Tetracontagon Number

    Given an integer N, the task is to check if it is a Tetracontagon number or not. Tetracontagon number is class of figurate number. It has 40 –…

    2 min read
  • Find max rational number in given Array

    Given the Numerator and Denominator of N Rational Numbers, the task is to find the maximum Rational Number from the array. Examples: Input: N = 2 num[] =…

    2 min read
  • Check if a Number is Pentacontagon

    Given an integer N, the task is to check if it is a pentacontagon number or not. Pentacontagon number is class of figurate number. It has 50 –…

    2 min read
  • Check if a Number is Triacontakaidigon

    Given an integer N, the task is to check if it is a Triacontakaidigon number or not. Triacontakaidigon number is class of figurate number. It has 32 –…

    2 min read
  • Check if a Number is Hexacontagon

    Given an integer N, the task is to check if it is a hexacontagon number or not. Hexacontagon number is class of figurate number. It has 60 –…

    2 min read