Given a string of lowercase characters only, the task is to check if it is possible to split a string from middle which will gives two halves having the same characters and same frequency of each character. If the length of the given string is ODD then ignore the middle element and check for the rest.
Input : abbaab Output : NO The two halves contain the same characters but their frequencies do not match so they are NOT CORRECT Input : abccab Output : YES
This problem has existing solution, please refer Check if both halves of the string have same set of characters link. We will solve this problem in Python quickly using Dictionary comparison.
Approach is very simple :
- Break string in two parts and convert both parts into dictionary using Counter(iterator) method, each dictionary contains it’s character as key and frequency as value.
- Now compare these two dictionaries. In python we can compare two using == operator, it first checks keys of both dictionaries are same or not, then it checks for values of each key. If everything is equal that means two dictionaries are identical.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if both halves of the string have same set of characters
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Check if both halves of the string have at least one different character
- Python | Ways to check string contain all same characters
- Python program to check if a string contains all unique characters
- Python | Split given string into equal halves
- Check whether second string can be formed from characters of first string
- Quick way to check if all the characters of a string are same
- Check if a given string is made up of two alternating characters
- Check whether the frequencies of all the characters in a string are prime or not
- Program to check if first and the last characters of string are equal
- Check if the characters of a given string are in alphabetical order
- Check if a string is made up of K alternating characters
- Check if characters of one string can be swapped to form other
- Check if a string has all characters with same frequency with one variation allowed
- Check if string follows order of characters defined by a pattern or not | Set 3
- Check if characters of a given string can be rearranged to form a palindrome
- Check whether count of distinct characters in a string is Prime or not
- Check if string follows order of characters defined by a pattern or not | Set 2
- Check if string follows order of characters defined by a pattern or not | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.