Challenge: Group Anagrams

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!

Create a free account to view this lesson.

Continue your learning journey with a 14-day free trial.

By signing up, you agree to Educative's Terms of Service and Privacy Policy