
- 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 both halves of the string have the same set of characters in Python
We have to check whether two halves of a string have the same set of characters or not in Python. The frequency of the characters in the two halves must be identical. If the length of the string is odd, ignore the middle and check for the remaining characters. Follow the below steps to write code for the program.
Algorithm
1. Initialize a string. 2. Initialize an empty dictionary variable alphabets. 3. Initialize a variable mid with length / 2. 4. Write a loop until mid element. 4.1. Initialize the corresponding dictionary item by alphabets[char] with one if it's not initialized. 4.2. If it's already initialized, increment the count by 1. 5. Run the loop from the mid element to the last item. 5.1. Check if the char is in the dictionary or not. 5.1.1. Decrement the count of char by one if it's in the dictionary 6. Run a loop over the dictionary alphabets. 6.1. If you find any item with more than 0 value. 6.1.1. Print **No!**. 6.2. Else print Yes!
Let's write the code.
Example
## initializing the string string = "aabccbaa" ## initializing an empty string alphabets = {} ## initializing the mid variable mid = len(string) // 2 ## loop to count the frequency of char in the first half for i in range(mid): ## setting the value of char count to 1 if it's not in the dictionary if not alphabets.get(string[i], 0): alphabets[string[i]] = 1 else: ## incrementing the count of char by 1 if it's already initialized alphabets[string[i]] += 1 ## loop to decrement the count of char by 1 if it's present in second half of the string for i in range(len(string) - 1, mid - 1, -1): ## checking whether the char is in second half or not if alphabets.get(string[i], 0): ## if it's present, decrementing the count by 1 alphabets[string[i]] -= 1 ## initializing a flag variable for the track flag = 1 ## loop to check the values after decrementing for i in alphabets.values(): ## checking for zeroes if i != 0: ## if it's not zero breaking the loop and printing No! print("No!") ## setting 0 for track flag = 0 break ## if flag value still 1 then, it's Yes! if flag == 1: ## printing Yes! print("Yes!")
Output
If you run the above program, you will get the following output.
Yes!
Conclusion
If you have any doubts regarding the tutorial, mention them in the comment section.
- Related Articles
- Check if both halves of the string have same set of characters in Python
- Python program to check if both halves of the string have same set of characters.
- Check if both halves of the string have same set of characters in C#
- Check if both halves of the string have at least one different character in Python
- Check if two String objects have the same value in C#
- Java Program to check if the String contains any character in the given set of characters
- Check if the characters of a given string are in alphabetical order in Python
- Python - Check If All the Characters in a String Are Alphanumeric?
- Python - Check if frequencies of all characters of a string are different
- Create a new string by alternately combining the characters of two halves of the string in reverse in C++ Program
- Check if frequency of all characters can become same by one removal in Python
- Check if a string has all characters with same frequency with one variation allowed in Python
- Program to check whether String Halves Are Alike in Python
- Check if lowercase and uppercase characters are in same order in Python
- Check if characters of one string can be swapped to form other in Python

Advertisements