
- 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
Check if any anagram of a string is palindrome or not in Python
Suppose we have a string s. We have to check whether an anagram of that string is forming a palindrome or not.
So, if the input is like s = "aarcrec", then the output will be True one anagram of this string is "racecar" which is palindrome.
To solve this, we will follow these steps −
- freq := a map to store all characters and their frequencies
- odd_count := 0
- for each f in list of all values of freq, do
- if f is odd, then
- odd_count := odd_count + 1
- if f is odd, then
- if odd_count > 1, then
- return False
- return True
Let us see the following implementation to get better understanding −
Example
from collections import defaultdict def solve(s): freq = defaultdict(int) for char in s: freq[char] += 1 odd_count = 0 for f in freq.values(): if f % 2 == 1: odd_count += 1 if odd_count > 1: return False return True s = "aarcrec" print(solve(s))
Input
"aarcrec"
Output
True
- Related Articles
- Python program to check if a string is palindrome or not
- Program to check a string is palindrome or not in Python
- C# program to check if a string is palindrome or not
- Program to check given string is anagram of palindromic or not in Python
- Check if number is palindrome or not in Octal in Python
- How to Check Whether a String is Palindrome or Not using Python?
- Program to check a number is palindrome or not without help of a string in Python
- Check if a string is Isogram or not in Python
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- Program to check string is palindrome or not with equivalent pairs in Python
- Program to check string is palindrome with lowercase characters or not in Python
- Program to check two parts of a string are palindrome or not in Python
- Python - Check if a given string is binary string or not
- Check if all elements of the array are palindrome or not in Python
- Check if a doubly linked list of characters is palindrome or not in C++

Advertisements