Sort an array of strings based on the given order

Given an array of strings words[] and the sequential order of alphabets, our task is to sort the array according to the order given. Assume that the dictionary and the words only contain lowercase alphabets.

Examples:

Input: words = {“hello”, “geeksforgeeks”}, order = “hlabcdefgijkmnopqrstuvwxyz”
Output: “hello”, “geeksforgeeks”
Explanation:
According to the given order ‘h’ occurs before ‘g’ and hence the words are considered to be sorted.

Input: words = {“word”, “world”, “row”}, order = “worldabcefghijkmnpqstuvxyz”
Output: “world”, “word”, “row”
Explanation:
According to the given order ‘l’ occurs before ‘d’ hence the words “world” will be kept first.

Approach: To solve the problem mentioned above we need to maintain the priority of each character in the given order. For doing that use Map Data Structure.



Below is the implementation of above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to sort an array
// of strings based on the given order
  
#include <bits/stdc++.h>
using namespace std;
  
// For storing priority of each character
unordered_map<char, int> mp;
  
// Custom comparator function for sort
bool comp(string& a, string& b)
{
  
    // Loop through the minimum size
    // between two words
    for (int i = 0;
         i < min(a.size(), b.size());
         i++) {
  
        // Check if the characters
        // at position i are different,
        // then the word containing lower
        // valued character is smaller
        if (mp[a[i]] != mp[b[i]])
            return mp[a[i]] < mp[b[i]];
    }
  
    /* When loop breaks without returning, it
       means the prefix of both words were same
       till the execution of the loop.
       Now, the word with the smaller size will
       occur before in sorted order
        */
    return (a.size() < b.size());
}
  
// Function to print the
// new sorted array of strings
void printSorted(vector<string> words,
                 string order)
{
  
    // Mapping each character
    // to its occurance position
    for (int i = 0; i < order.size(); i++)
        mp[order[i]] = i;
  
    // Sorting with custom sort function
    sort(words.begin(), words.end(), comp);
  
    // Printing the sorted order of words
    for (auto x : words)
        cout << x << " ";
}
  
// Driver code
int main()
{
  
    vector<string> words
        = { "word", "world", "row" };
    string order
        = { "worldabcefghijkmnpqstuvxyz" };
  
    printSorted(words, order);
  
    return 0;
}
chevron_right

Output:
world word row

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.





Check out this Author's contributed articles.

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 :