Given a sequence of words, print all anagrams together using STL

Given an array of words, print all anagrams together. 
For example, 

Input: array = {“cat”, “dog”, “tac”, “god”, “act”}
output: cat tac act, dog god
Explanation: cat tac and act are anagrams 
and dog and god are anagrams as 
they have the same set of characters.

Input: array = {“abc”, “def”, “ghi”}
output: abc, def, ghi
Explanation: There are no anagrams in the array.


Other approaches are discussed herein these posts: 

Approach: This is a HashMap solution using C++ Standard Template Library which stores the Key-Value Pair. In the hashmap, the key will be the sorted set of characters and value will be the output string. Two anagrams will be similar when their characters are sorted. Now, 

  1. Store the vector elements in HashMap with key as the sorted string.
  2. If the key is same, then add the string to the value of HashMap(string vector).
  3. Traverse the HashMap and print the anagram strings.







// CPP program for finding all anagram
// pairs in the given array
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
// Utility function for
// printing anagram list
void printAnagram(
                  vector<string> >& store)
                  vector<string> >::iterator it;
    for (it = store.begin();
         it != store.end(); it++) {
        vector<string> temp_vec(it->second);
        int size = temp_vec.size();
        if (size > 1) {
            for (int i = 0; i < size; i++) {
                cout << temp_vec[i] << " ";
            cout << "\n";
// Utility function for storing
// the vector of strings into HashMap
void storeInMap(vector<string>& vec)
                  vector<string> >
    for (int i = 0; i < vec.size(); i++) {
        string tempString(vec[i]);
        // Check for sorted string
        // if it already exists
        if (store.find(
            == store.end()) {
            vector<string> temp_vec;
                tempString, temp_vec));
        else {
            // Push new string to
            // already existing key
            vector<string> temp_vec(
            store[tempString] = temp_vec;
    // print utility function for printing
    // all the anagrams
// Driver code
int main()
    // initialize vector of strings
    vector<string> arr;
    // utility function for storing
    // strings into hashmap
    return 0;


Note: Compile above program with -std=c++11 flag in g++ 

cat act tca 
geeksforgeeks forgeeksgeeks 
geeksquiz zuiqkeegs 

Complexity Analysis: 

  • Time Complexity: O(n * m(log m)),  where m is the length of a word.
    A single traversal through the array is needed.
  • Space Complexity: O(n). 
    There are n words in a string. The map requires O(n) space to store the strings.

This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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

Improved By : andrew1234, ssharan3112