</>

49. Group Anagrams

Medium NeetCode 150
Arrays & Hashing Time O(N * K log K) Space O(N * K)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Arrays & Hashing
Time
O(N * K log K)
Space
O(N * K)

Standard Solution (C++)

SummaryGiven a list of strings, group together all words that are anagrams of each other and return the collection of groups in any order.

Approach
  • Use a hash map keyed by a canonical form of each string so that anagrams share the same key.
  • The canonical form here is a fixed-length count array of 26 lowercase-letter frequencies, serialized to a string — O(K) per word instead of O(K log K) for sorting.
  • Iterate over every input string, build its key, and append it to the matching bucket in the map.
  • Collect all map values into the result vector.
#include <vector>
#include <string>
#include <unordered_map>
#include <array>
using namespace std;

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        // Key: 26-char frequency fingerprint; Value: all strings sharing that fingerprint
        unordered_map<string, vector<string>> groups;

        for (const string& s : strs) {
            // Build a character-count key in O(K) — avoids the O(K log K) sort
            array<int, 26> freq{};
            for (char c : s) ++freq[c - 'a'];

            // Serialize the count array into a fixed-length string key
            string key(freq.begin(), freq.end());
            groups[key].push_back(s);
        }

        // Collect each bucket into the result
        vector<vector<string>> result;
        result.reserve(groups.size());
        for (auto& [key, bucket] : groups)
            result.push_back(move(bucket));

        return result;
    }
};
Time O(N * K) where N is the number of strings and K is the maximum string length Space O(N * K) for the hash map and result storage

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        std::unordered_map<string, vector<string>> umap;
        for(const string& s: strs){
            string t=s;
            sort(t.begin(), t.end());
            umap[t].push_back(s);
        }

        vector<vector<string>> res;
        for(const auto p: umap){
            res.push_back(p.second);
        }

        return res;
    }
};

// Time Complexity: O(N * K log K) where N is the number of strings and K is the maximum length of a string
// Space Complexity: O(N * K)
Time O(N * K log K) Space O(N * K)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern