
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- Anagram checking in Python using collections.Counter()
- An Anagram I Am in Python
- Anagram Substring Search using Python
- Valid Sudoku in Python
- Valid Palindrome in Python
- Valid Number in Python
- Anagram checking in Python program using collections.Counter()
- Python Program for Anagram Substring Search
- Valid Mountain Array in Python
- Longest Valid Parentheses in Python
- What are valid python identifiers?
- Program to find length of longest anagram subsequence in Python
- Check if binary representations of two numbers are anagram in Python
- Check whether two strings are anagram of each other in Python
- Minimum Add to Make Parentheses Valid in Python

Advertisements