Anagram Detection Challenge

PythonPythonBeginner
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" is an anagram of "silent". In this challenge, you will be asked to write a function that checks if two strings are anagrams of each other.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) 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/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") 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`") subgraph Lab Skills python/comments -.-> lab-13134{{"`Anagram Detection Challenge`"}} python/booleans -.-> lab-13134{{"`Anagram Detection Challenge`"}} python/conditional_statements -.-> lab-13134{{"`Anagram Detection Challenge`"}} python/for_loops -.-> lab-13134{{"`Anagram Detection Challenge`"}} python/tuples -.-> lab-13134{{"`Anagram Detection Challenge`"}} python/function_definition -.-> lab-13134{{"`Anagram Detection Challenge`"}} python/importing_modules -.-> lab-13134{{"`Anagram Detection Challenge`"}} python/using_packages -.-> lab-13134{{"`Anagram Detection Challenge`"}} python/standard_libraries -.-> lab-13134{{"`Anagram Detection Challenge`"}} end

String Anagram Challenge

Problem

Write a function is_anagram(s1, s2) that takes two strings as arguments and returns True if they are anagrams of each other, and False otherwise. The function should be case-insensitive, ignore spaces, punctuation, and special characters.

To solve this problem, you can follow these steps:

  1. Use str.isalnum() to filter out non-alphanumeric characters and str.lower() to transform each character to lowercase.
  2. Use collections.Counter to count the resulting characters for each string and compare the results.

Example

is_anagram('#anagram', 'Nag a ram!')  ## True
is_anagram('hello', 'world')  ## False

Summary

In this challenge, you have learned how to check if two strings are anagrams of each other. You have used str.isalnum() to filter out non-alphanumeric characters, str.lower() to transform each character to lowercase, and collections.Counter to count the resulting characters for each string and compare the results.

Other Python Tutorials you may like