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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Check if both halves of the string have same set of characters
- Check if both halves of the string have at least one different character
- Check if both halves of a string are Palindrome or not
- Partition the string in two parts such that both parts have at least k different characters
- Count ways to partition a string such that both parts have equal distinct characters
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Check whether two strings contain same characters in same order
- Python | Split given string into equal halves
- Print all positions of a given string having count of smaller characters equal on both sides
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Python | Ways to check string contain all same characters
- Program to check if all characters have even frequency
- Rearrange the given string such that all prime multiple indexes have same character
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Minimum characters to be replaced to make frequency of all characters same
- Check if a string has all characters with same frequency with one variation allowed
- Check if a string can be split into two strings with same number of K-frequent characters
- Quick way to check if all the characters of a string are same
- Python | Find whether all tuple have same length
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.