bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to Group Anagrams
🧩Arrange CodecppMediumArrays & Hashing

Group Anagrams - Arrange Code (C++)

Put the C++ code in order

Problem Brief

Given an array of strings strs, group the anagrams together. You can return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Puzzle Hints
  1. Ignore visual position first; place the line that creates required state before lines that read it. One candidate is "}".

  2. Arrange the cpp code lines in the correct order to form a working solution.

  3. When two lines both look plausible, choose the one whose output is needed by the next line.

Asked at 36 companies
AdobeAffirmAmazon
Group Anagrams — Sorted Key HashMap
hashmap
1
8

Input: ["eat","tea","tan","ate","nat","bat"]. Key = sorted chars

unordered_map & unordered_set in C++ref

C++ unordered_map and unordered_set use hash tables for O(1) average operations. Include <unordered_map> and <unordered_set>. For sorted order, use map/set (O(log n) red-black tree).

-unordered_map<K,V> — O(1) insert/find/erase
-unordered_set<T> — O(1) insert/count/erase
-operator[] auto-inserts default if missing
-count(key) returns 0 or 1 (use as boolean)
#include <unordered_map>
#include <unordered_set>

unordered_map<string, int> m;
m["a"] = 1;
m.count("a");  // 1 (exists)

unordered_set<int> s = {1, 2, 3};
s.count(2);    // 1
Official docs →
unordered_map & unordered_set in C++ref

C++ unordered_map and unordered_set use hash tables for O(1) average operations. Include <unordered_map> and <unordered_set>. For sorted order, use map/set (O(log n) red-black tree).

-unordered_map<K,V> — O(1) insert/find/erase
-unordered_set<T> — O(1) insert/count/erase
-operator[] auto-inserts default if missing
-count(key) returns 0 or 1 (use as boolean)
#include <unordered_map>
#include <unordered_set>

unordered_map<string, int> m;
m["a"] = 1;
m.count("a");  // 1 (exists)

unordered_set<int> s = {1, 2, 3};
s.count(2);    // 1
Official docs →
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
  • 1 }
  • 2class Solution {
  • 3 result.push_back(group);
  • 4 string key = s;
  • 5 unordered_map<string, vector<string>> map;
  • 6 map[key].push_back(s);
  • 7public:
  • 8 for (auto& [key, group] : map) {
  • 9 return result;
  • 10 vector<vector<string>> groupAnagrams(vector<string>& strs) {
  • 11 for (const string& s : strs) {
  • 12 }
  • 13 vector<vector<string>> result;
  • 14 sort(key.begin(), key.end());
  • 15};
  • 16 }