
- 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 characters of a given string can be rearranged to form a palindrome in Python
Suppose we have a string s, we have to check whether characters of the given string can be shuffled to make a palindrome or not.
So, if the input is like s = "raaecrc", then the output will be True as we can rearrange this to "racecar" which is a palindrome.
To solve this, we will follow these steps −
- freq := a map to store all characters and their frequencies in s
- odd_count := 0
- for each element i in the list of all values of freq, do
- if i is odd, then
- odd_count := odd_count + 1
- if odd_count > 1, then
- return False
- if i is odd, then
- return True
Let us see the following implementation to get better understanding −
Example
from collections import defaultdict def solve(st) : freq = defaultdict(int) for char in st : freq[char] += 1 odd_count = 0 for i in freq.values(): if i % 2 == 1: odd_count = odd_count + 1 if odd_count > 1: return False return True s = "raaecrc" print(solve(s))
Input
"raaecrc"
Output
True
- Related Articles
- Check if a string can be rearranged to form special palindrome in Python
- Check if characters of one string can be swapped to form other in Python
- Can part of a string be rearranged to form another string in JavaScript
- Check if the characters in a string form a Palindrome in O(1) extra space in Python
- Find the count of sub-strings whose characters can be rearranged to form the given word in Python
- Python program to check if a given string is number Palindrome
- Check if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript
- C Program to Check if a Given String is a Palindrome?
- Check if a given string is a rotation of a palindrome in C++
- Check if a string can be formed from another string using given constraints in Python
- C++ program to find how many characters should be rearranged to order string in sorted form
- Python program to check if the given string is vowel Palindrome
- Check if it is possible to create a palindrome string from given N in Python
- Check if the characters of a given string are in alphabetical order in Python
- Check if a two-character string can be made using given words in Python

Advertisements