- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a string can be rearranged to form special palindrome in Python
Suppose we have a string; 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 = "aarcrce", then the output will be True as we can shuffle characters to form "racecar" which is a palindrome.
To solve this, we will follow these steps −
- size := 256
- freq := an array of size 256 and fill with 0
- for i in range 0 to size of s, do
- increse frequency of character s[i] in freq array by 1
- odd_count := 0
- for i in range 0 to size, do
- if freq[i] is idd, then
- odd_count := odd_count + 1
- if odd_count > 1, then
- return False
- if freq[i] is idd, then
- return True
Let us see the following implementation to get better understanding −
Example
size = 256 def solve(s) : freq = [0] * size for i in range( 0, len(s)) : freq[ord(s[i])] = freq[ord(s[i])] + 1 odd_count = 0 for i in range(0, size) : if freq[i] % 2 == 1 : odd_count = odd_count + 1 if odd_count > 1: return False return True s = "aarcrce" print(solve(s))
Input
"aarcrce"
Output
True
- Related Articles
- Check if characters of a given string can be rearranged to form a palindrome in Python
- Can part of a string be rearranged to form another string in JavaScript
- Check if characters of one string can be swapped to form other in Python
- Check if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript
- Check if a string can be repeated to make another string in Python
- Check if the characters in a string form a Palindrome in O(1) extra space in Python
- Checking if a string can be made palindrome in JavaScript
- Program to check subarrays can be rearranged from arithmetic sequence or not in Python
- Python program to check if a string is palindrome or not
- Python program to check if a given string is number Palindrome
- Program to check if a string contains any special character in Python
- How to check if a string can be converted to float in Python?
- How to write a JavaScript function that returns true if a portion of string 1 can be rearranged to string 2?
- Python program to check if the given string is vowel Palindrome
- Check if a string can be formed from another string using given constraints in Python

Advertisements