Anagram program in python – Python Program to Check If Two Strings are Anagram

String:

Anagram program in python: Arrays are Strings. Strings in Python, like many other common programming languages, are sequences of bytes that represent unicode characters. However, since Python lacks a character data form, a single character is simply a one-length string. Square brackets may be used to access the string’s components.

Anagrams:

Anagram in python: A condition in which one string or number is rearranged in such a way that each character of the rearranged string or number must be a part of another string or number is known as an anagram. In other words, if the second string is a simple rearrangement of the first, it is said to be an anagram of the first.

Example :1)The words cricket and crikte are anagrams

2)The words pink and nipk.

Note :

Check if two strings are anagrams: Here we can ignore the case while checking anagrams like  BTechGeeks and geeksechtb are anagrams by ignoring the case.

Examples:

Example1:

Input:

string 1 = "skyis"    string2= "ssyki"

Output:

Both the strings are anagrams

Explanation:

Here these two strings are equal after rearranging the letters.

Example2:

Input:

string 1 = "kaswpink"    string2= "krwsa"

Output:

Both the strings are not anagrams

Explanation:

Here these two strings are not equal after rearranging the letters and also both are not having same number
of letters.

Example3:

Input:

string 1 = "BTechGeeks"   string 2 = "geeksechtb"

Output:

Both the strings are anagrams

Explanation:

Here both the strings are same by rearranging the words and by ignoring the case of it.

Checking If Two Strings are Anagrams in Python

Check if two strings are anagrams java: Below are the ways to check if both the strings are anagrams.

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1:Using sorted() method

Approach:

  • Scan the both strings.
  • Convert the both strings to lowercase using lower() function since we ignore the case.
  • Then sort both strings  with sorted().
  • Check if the sorted strings are identical by comparing them.
  • If both the strings are equal then they are anagrams
  • Else both the strings are not anagrams.

Below is the implementation:

# given two strings
string1 = "skyis"
string2 = "ssyki"
# converting the both strings to lowercase using lower() function.
string1 = string1.lower()
string2 = string2.lower()
# checking if both the strings are equal using sorted() function
if(sorted(string1) == sorted(string2)):
    print("Both the strings are anagrams")
else:
    print("Both the strings are not anagrams")

Output:

Both the strings are anagrams

Explanation:

  • Both strings must be entered and stored in different variables.
  • Use the lower() and upper() functions to convert all strings to lowercase or uppercase.
  • Since we don’t consider the case when comparing,
  • Both strings’ characters are separated into different lists.
  • Using an if statement, they are then compared to see if they are equal.
  • They’re anagrams if they’re identical and the characters are literally jumbled in anagrams.
  • The strings aren’t anagrams if they aren’t equal.

Method #2:Using Counter() function

Counter:

The Counter class in Python3 is a special type of object data-set that comes with the collections module. The Collections module offers specialised container datatypes as an alternative to Python’s general-purpose built-ins such as dictionaries, lists, and tuples.

Hashable objects are counted using the Counter subclass. When called, it generates an iterable hash table implicitly.

Approach:

  • Scan the both strings.
  • Convert the both strings to lowercase using lower() function since we ignore the case.
  • Find the frequency of each character in the string using Counter function.
  • Repeat the same for second string.
  • If both the frequencies and keys are equal then it is anagram.
  • Else it is not anagram.
  • We can check if both frequencies are equal using == operator.

Below is the implementation:

# importing counter from collections
from collections import Counter
# given two strings
string1 = "skyis"
string2 = "ssyki"
# converting the both strings to lowercase using lower() function.
string1 = string1.lower()
string2 = string2.lower()
# finding the frequencies of both string using Counter()
freqstring1 = Counter(string1)
freqstring2 = Counter(string2)
# checking if both the strings are equal by comparing the frequencies.
if(freqstring1 == freqstring2):
    print("Both the strings are anagrams")
else:
    print("Both the strings are not anagrams")

Output:

Both the strings are anagrams

Related Programs: