Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,945 questions

51,887 answers

573 users

How to find groups of anagrams of a given words in C++

2 Answers

0 votes
#include <vector>
#include <algorithm>
#include <iostream>
#include <unordered_map>

std::vector<std::vector<std::string>> groupAnagrams(std::vector<std::string>& vec) {
    std::unordered_map<std::string, std::vector<std::string>> map;
    std::vector<std::vector<std::string>> groups;
    
    for (int i = 0; i < vec.size(); i++) {
        std::string word = vec[i];
        sort(word.begin(), word.end());
        map[word].push_back(vec[i]);
    }
    
    for (auto x : map) {
        groups.push_back(x.second);
    }
    
    return groups;
}

int main()
{
    std::vector<std::string> vec;

    vec.push_back("demo"); 
    vec.push_back("are"); 
    vec.push_back("code"); 
    vec.push_back("ear");
    vec.push_back("trap");
    vec.push_back("rapt");
    vec.push_back("dome");
    vec.push_back("part");
    vec.push_back("mode");
    
    std::vector<std::vector<std::string>> groups =  groupAnagrams(vec);

    for (int i = 0; i < groups.size(); i++) {
        if (groups[i].size() > 0) {
            for (int j = 0; j < groups[i].size(); j++)
                std::cout << groups[i][j] << " ";
        }
        std::cout << "\n";
    }
}



/*
run:

trap rapt part 
code 
are ear 
demo dome mode 

*/

 



answered Jan 16, 2023 by avibootz
edited Jan 16, 2023 by avibootz
0 votes
#include <vector>
#include <algorithm>
#include <iostream>
#include <unordered_map>

using std::vector;
using std::string;

vector<vector<string>> groupAnagrams(vector<string>& vec) {
    std::unordered_map<string, vector<string>> map;
    vector<vector<string>> groups;
    
    for (int i = 0; i < vec.size(); i++) {
        string word = vec[i];
        sort(word.begin(), word.end());
        map[word].push_back(vec[i]);
    }
    
    for (auto x : map) {
        groups.push_back(x.second);
    }
    
    return groups;
}

int main()
{
    vector<string> vec;

    vec.push_back("demo"); 
    vec.push_back("are"); 
    vec.push_back("code"); 
    vec.push_back("ear");
    vec.push_back("trap");
    vec.push_back("rapt");
    vec.push_back("dome");
    vec.push_back("part");
    vec.push_back("mode");
    
    vector<vector<string>> groups =  groupAnagrams(vec);

    for (int i = 0; i < groups.size(); i++) {
        if (groups[i].size() > 0) {
            for (int j = 0; j < groups[i].size(); j++)
                std::cout << groups[i][j] << " ";
        }
        std::cout << "\n";
    }
}



/*
run:

trap rapt part 
code 
are ear 
demo dome mode 

*/

 



answered Jan 16, 2023 by avibootz

Related questions

...