Check if Two Strings are Anagram - Python Last Updated : 21 Feb, 2025 Comments Improve Suggest changes Like Article Like Report The task of checking if two strings are anagrams in Python involves determining whether the two strings contain the same characters with the same frequency, but possibly in a different order. For example, given the strings "listen" and "silent", they are anagrams because both contain the same characters with identical frequencies, resulting in the output "Yes".Using HashmapThis is the most efficient and preferred method to check for anagrams. It uses a hashmap (dictionary) to count the frequency of each character in both strings and compares the two frequency maps. If both strings have identical character frequencies, they are anagrams. Python from collections import Counter # for character frequency counting s1 = "listen" s2 = "silent" if len(s1) != len(s2): # check length print("No") else: if Counter(s1) == Counter(s2): # compare character counts print("Yes") else: print("No") OutputYes Explanation: If the lengths are equal, Counter(s1) and Counter(s2) create frequency dictionaries for both strings. If they match, it prints "Yes" otherwise, it prints "No".Table of ContentUsing arrayUsing sorted()Using arrayWe know that the strings contain only lowercase English letters (a-z), we can use an array of size 26 to track the frequency of each character instead of using a hashmap. Each character is mapped to an index between 0 and 25 using the ord() function. Python s1 = "listen" s2 = "silent" if len(s1) != len(s2): # check length print("No") else: count = [0] * 26 # array to track character frequencies (a-z) for i in range(len(s1)): count[ord(s1[i]) - ord('a')] += 1 # increment for s1 count[ord(s2[i]) - ord('a')] -= 1 # decrement for s2 if all(x == 0 for x in count): # check if all frequencies are zero print("Yes") else: print("No") OutputYes Explanation: If the lengths are the same, an array count of size 26 is initialized to track letter frequencies. A loop increments indices for characters in s1 and decrements for s2. If all values in count are zero after the loop, it prints "Yes" otherwise, "No."Using sorted()This is the most efficient and simple method to check for anagrams. The idea is to sort both strings and compare the sorted versions. If both sorted strings are equal, they are anagrams. Python s1 = "listen" s2 = "silent" if sorted(s1) == sorted(s2): print("Yes") else: print("No") OutputYes Explanation: If the sorted versions of both strings are equal, it prints "Yes," indicating they are anagrams otherwise, it prints "No." Comment More infoAdvertise with us Next Article Check if Two Strings are Anagram - Python S Striver Follow Improve Article Tags : Misc Strings Python Python Programs DSA anagram Python-Sorted +3 More Practice Tags : anagramMiscpythonStrings Similar Reads Python Program To Check Whether Two Strings Are Anagram Of Each Other Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, "abcd" and "dabc" are an anagram of each other. We strongly recommend that you 8 min read Check for ASCII String - Python To check if a string contains only ASCII characters, we ensure all characters fall within the ASCII range (0 to 127). This involves comparing each character's value to ensure it meets the criteria.Using str.isascii()The simplest way to do this in Python is by using the built-in str.isascii() method, 2 min read Python - Check if two strings are Rotationally Equivalent Sometimes, while working with Python Strings, we can have problem in which we need to check if one string can be derived from other upon left or right rotation. This kind of problem can have application in many domains such as web development and competitive programming. Let's discuss certain ways i 4 min read Python | Ways to check string contain all same characters Given a list of strings, write a Python program to check whether each string has all the characters same or not. Given below are a few methods to check the same. Method #1: Using Naive Method [Inefficient] Python3 # Python code to demonstrate # to check whether string contains # all characters same 5 min read Python Program to check for almost similar Strings Given two strings, the task here is to write a python program that can test if they are almost similar. Similarity of strings is being checked on the criteria of frequency difference of each character which should be greater than a threshold here represented by K. Input : test_str1 = 'aabcdaa', test 5 min read Python Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples : Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output 3 min read Python | Union Operation in two Strings One of the string operation can be computing the union of two strings. This can be useful application that can be dealt with. This article deals with computing the same through different ways. Method 1 : Naive Method The task of performing string union can be computed by naive method by creating an 5 min read Test if String is Subset of Another - Python We are given two strings s1 and s2 and the task is to check whether s2 is a subsequence of s1. A subsequence is a sequence that appears in the same relative order but not necessarily consecutively. Using all() In this method we check whether all the characters of s2 are present in s1 by using the al 3 min read Python - String uncommon characters One of the string operation can be computing the uncommon characters of two strings i.e, output the uncommon values that appear in both strings. This article deals with computing the same in different ways. Method 1: Using set() + symmetric_difference() Set in python usually can perform the task of 5 min read How to Compare Two Dictionaries in Python In this article, we will discuss how to compare two dictionaries in Python. The simplest way to compare two dictionaries for equality is by using the == operator.Using == operatorThis operator checks if both dictionaries have the same keys and values.Pythond1 = {'a': 1, 'b': 2} d2 = {'a': 1, 'b': 2} 2 min read Like