Check if both halves of the string have same set of characters in Python

In Python, a string is one of the data structures that is a sequence of characters enclosed within single quotes '' or double quotes "". It is immutable, i.e., once a string is created, it cannot be changed.

When we want to check if both halves of the string have the same set of characters in Python, we can follow the steps below −

  • First, based on the length of the string, we have to split the string into two halves.
  • Next, convert each half into a set of characters using set() function.
  • Finally, compare the two sets using the equality operator ==.

Using set() Function

set() is a built-in function in Python that creates an unordered collection of unique elements. Sets are mutable, but the elements contained in a set must be immutable. This function takes a string, list, tuple, etc, as input.

Example

In this example, we will check for the same set of characters in both halves of a string with the help of the set() function by passing the string as the input ?

def check_same_characters_in_halves(s):
    length = len(s)
    
    # If length is odd, ignore the middle character
    mid = length // 2
    first_half = set(s[:mid])
    second_half = set(s[-mid:])
    
    return first_half == second_half

# Test cases
print(check_same_characters_in_halves("abcabc"))    # True
print(check_same_characters_in_halves("aabbcc"))    # False
print(check_same_characters_in_halves("abcdab"))    # False
print(check_same_characters_in_halves("abcdeabc"))  # False

The output of the above code is ?

True
False
False
False

Using collections.Counter Class

collections.Counter is a class in Python's collections module, which is used to count the frequency of elements in an iterable. It returns a dictionary-like object where elements are stored as dictionary keys and their counts as dictionary values.

Example

In this example, we will check for the same characters and their frequencies in both halves of a string with the help of collections.Counter class by passing string slices as input ?

from collections import Counter

def check_same_character_frequencies(s):
    length = len(s)
    
    # If length is odd, ignore the middle character
    mid = length // 2
    first_half = s[:mid]
    second_half = s[-mid:]

    return Counter(first_half) == Counter(second_half)

# Test cases
print(check_same_character_frequencies("tutorialspoint"))  # False
print(check_same_character_frequencies("TutTut"))          # True
print(check_same_character_frequencies("Learning"))        # False
print(check_same_character_frequencies("python"))          # False

The output of the above code is ?

False
True
False
False

Comparison

Method Checks Best For
set() Unique characters only When frequency doesn't matter
Counter() Characters and frequencies When exact character count matters

Conclusion

Use set() to check if both halves contain the same unique characters. Use Counter() when you need to verify both characters and their exact frequencies match in both halves.

Updated on: 2026-03-25T06:39:04+05:30

431 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements