Email: rakesh@xamnation.com         Phone/Whatsapp: +91 9988708161
interview questions

Program to Detect if Two Strings are Anagrams

Hello

In the article, we will show how to find whether numbers are anagram. This article is part of interview preparation series.

What is anagram?

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase in different order

For example: team and meat are anagram. Secure and rescue are anagram

How to check whether 2 words or anagram

To check out whether 2 words or phrases are anagram, you need to check whether each letter or words are common .

TEAM= T,E,A,M

MEAT = M,E,A,T

As you can see all of the characters are same, and just the differently arranged in both words.

Python code to find 2 words are anagram

def findAnagram(str1,str2):
	if(len(str1)!=len(str2)):
		return "Not found"

	str_arr1 =list(str1)
	str_arr2 = list(str2)
	j=0
	while(j<len(str_arr1)):
		if(str_arr1[j] in str_arr2):
			str_arr2.remove(str_arr1[j])
		else:
			return "Not found"
		j+=1
	return "Found"

print(findAnagram("meat","team"))

End note

In the article, you have seen how to check anagram in string in Python.

If in case you have doubt regarding implementation, please feel free to share us your concerns in discussion thread below, or mail us to info@xamnation.com

This article is for technical interview preparation. Find more useful programming exercises in link below.

  • How to check whether string is palindrome or not
  • How to find prime numbers between a given range
  • How to check whether number is Armstrong number

Xamnation is helping students in interview preparation and learning programming language. We are offering live classes, study material and interview preparation course. Please find below useful course.

  • Aptitude preparation course for job interviews
  • Puzzles course for job interviews
  • Python course for job interviews
  • Amazon job preparation course
  • TCS job preparation course

Leave a Comment

Your email address will not be published. Required fields are marked *