VOOZH about

URL: https://www.geeksforgeeks.org/dsa/given-a-sequence-of-words-print-all-anagrams-together-using-stl/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 29 Oct, 2023

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.
Recommended Practice

Other approaches are discussed herein these posts:  

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.

Implementation:


Output
cat act tca 
abcd 
geeksforgeeks forgeeksgeeks 
geeksquiz zuiqkeegs 

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

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.

Another Approach: This program is designed to group anagrams from a given array of strings. The approach taken in this program is as follows:

  1. Define a function storeInMap that takes a vector of strings as input and creates an unordered_map object to store the anagrams.
  2. Iterate through each string in the vector and count the frequency of each character in the string.
  3. Generate a hash string based on the frequency of characters for each string.
  4. Insert the string into the unordered_map object using the hash string as the key.
  5. Define another function printAnagram that takes an unordered_map object as input and prints the anagrams.
  6. Iterate through the unordered_map object and print the anagrams using the values associated with each key.
  7. In the main function, create a vector of strings containing the input data and call the storeInMap function to group the anagrams.

Implementation:


Output
cat act tca 
abcd 
geeksforgeeks forgeeksgeeks 
geeksquiz zuiqkeegs 

Time Complexity: The time complexity of this code is O(n*m), where n is the number of strings in the given vector and m is the length of the longest string.
Auxiliary Space: The space complexity of this code is also O(n*m) because we are storing all the strings in the given vector in the store map.

This article is contributed by Mandeep Singh.  

Comment