Search⌘ K
AI Features

Challenge: Group Anagrams

Explore how to identify and group anagrams from an array of strings using sorting and searching methods. This lesson helps you design and implement algorithms that handle spaces as characters and return grouped anagrams effectively.

Problem Statement

Given an array of strings that contains anagrams, write a function to print pairs of those anagrams.

Input

An array of strings. Remember that spaces count as characters! So " abc" and “cab” are technically not anagrams since " abc" has spaces which “cab” does not.

Output

A string vector where all the anagrams are grouped together.

Sample Input

string arr[] = {
    "tom marvolo riddle ",
    "abc",
    "def",
    "cab",
    "fed",
    "brag",
    "clint eastwood ",
    "i am lord voldemort",
    "elvis",
    "grab",
    "old west action",
    "lives"
  };

Sample Output

vector <string> vec = {
    "clint eastwood",
    "old west action", 
    "tom marvolo riddle",
    "i am lord voldemort",
    "elvis",
    "lives",
    "def",
    "fed",
    "abc",
    "cab",
    "brag",
    "grab" 
  };

Coding Exercise

First take a close look and design a step-by-step algorithm, then jump on to implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the hint and solution provided in the code tab. Good Luck!

C++
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
vector<string> groupAnagrams(string strArray[], int arrSize) {
vector<string> vec;
// Write your code here
return vec;
}