 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Valid Anagram in Python
Anagrams are basically all permutations of a given string or pattern. This pattern searching algorithm is slightly different. In this case, not only the exact pattern is searched, it searches all possible arrangements of the given pattern in the text. So if the inputs are “ANAGRAM” and “NAAGARM”, then they are anagram, but “cat” and “fat” are not an anagram
To solve this, we will convert the string into a list of characters, then sort them, if two sorted lists are same then they are anagram.
Example (Python)
Let us see the following implementation to get a better understanding −
class Solution(object):    def isAnagram(self, s, t):       """       :type s: str       :type t: str       :rtype: bool       """       return "".join(sorted(s)) == "".join(sorted(t)) ob1 = Solution() print(ob1.isAnagram("ANAGRAM","NAAGARM"))  Input
s = "ANAGRAM" t = "NAAGARM"
Output
true
Advertisements
 