Anagram Sorting Array

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase. For example, "listen" and "silent" are anagrams of each other. In this challenge, we will be sorting an array of strings so that all anagrams are next to each other.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") algorithm/BasicAlgorithmsGroup -.-> algorithm/sorting_searching("`Sorting Searching`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-268873{{"`Anagram Sorting Array`"}} algorithm/sorting_searching -.-> lab-268873{{"`Anagram Sorting Array`"}} python/variables_data_types -.-> lab-268873{{"`Anagram Sorting Array`"}} python/conditional_statements -.-> lab-268873{{"`Anagram Sorting Array`"}} python/for_loops -.-> lab-268873{{"`Anagram Sorting Array`"}} python/lists -.-> lab-268873{{"`Anagram Sorting Array`"}} python/tuples -.-> lab-268873{{"`Anagram Sorting Array`"}} python/function_definition -.-> lab-268873{{"`Anagram Sorting Array`"}} python/importing_modules -.-> lab-268873{{"`Anagram Sorting Array`"}} python/using_packages -.-> lab-268873{{"`Anagram Sorting Array`"}} python/standard_libraries -.-> lab-268873{{"`Anagram Sorting Array`"}} python/classes_objects -.-> lab-268873{{"`Anagram Sorting Array`"}} python/encapsulation -.-> lab-268873{{"`Anagram Sorting Array`"}} python/raising_exceptions -.-> lab-268873{{"`Anagram Sorting Array`"}} python/data_collections -.-> lab-268873{{"`Anagram Sorting Array`"}} python/build_in_functions -.-> lab-268873{{"`Anagram Sorting Array`"}} end

Anagrams

Problem

Given an array of strings, write a function to sort the array so that all anagrams are next to each other. An anagram is defined as a word or phrase formed by rearranging the letters of another word or phrase. For example, "act" and "cat" are anagrams of each other.

Requirements

To solve this problem, the following requirements must be met:

  • The function must group all anagrams together in the sorted array.
  • There are no other sorting requirements other than the grouping of anagrams.
  • The inputs may not be valid, so the function must handle invalid inputs.
  • The function must fit in memory.

Example Usage

The function should behave as follows in these scenarios:

  • None -> Exception
  • [] -> []
  • General case
    • Input: ['ram', 'act', 'arm', 'bat', 'cat', 'tab']
    • Result: ['arm', 'ram', 'act', 'cat', 'bat', 'tab']

Summary

In this challenge, we learned how to group anagrams together in a sorted array of strings. By following the requirements and using the example usage, we can ensure that our function works as expected.

Other Algorithm Tutorials you may like