Check if both halves of the string have same set of characters in Python Last Updated : 10 Apr, 2023 Comments Improve Suggest changes Like Article Like Report 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. Examples: 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. Implementation: Python3 # Function to Check if both halves of # the string have same set of characters from collections import Counter def checkTwoHalves(input): length = len(input) # Break input string in two parts if (length % 2 != 0): first = input[0:length // 2] second = input[(length // 2) + 1:] else: first = input[0:length // 2] second = input[length // 2:] # Convert both halves into dictionary and compare if Counter(first) == Counter(second): print ('YES') else: print ('NO') # Driver program if __name__ == "__main__": input = 'abbaab' checkTwoHalves(input) OutputNO Time Complexity: O(n)Auxiliary Space: O(n) Approach#2: using sorted() This approach checks if both halves of a given string have the same set of characters. It first checks if the length of the string is odd, in which case the answer is "NO". Otherwise, it sorts the left and right halves of the string and checks if they are equal. If they are, the answer is "YES", otherwise it is "NO". Algorithm 1. Divide the input string into two halves, left and right.2. If the length of the string is odd, return "NO" because it is not possible to divide the string into two halves of equal length.3. Sort both halves and compare them. Python3 def same_set_of_chars(s): n = len(s) if n % 2 == 1: return "NO" left = sorted(s[:n//2]) right = sorted(s[n//2:]) return "YES" if left == right else "NO" # Example usage s = "abccab" print(same_set_of_chars(s)) # Output: YES OutputYES Time Complexity: O(n log n), where n is the length of the input string.Space Complexity: O(n), where n is the length of the input string. Comment More infoAdvertise with us Next Article Check if both halves of the string have same set of characters in Python S Shashank Mishra Follow Improve Article Tags : Strings Python DSA python-dict Practice Tags : pythonpython-dictStrings Similar Reads Check if both halves of the string have same set of characters Given a string of lowercase characters only, the task is to check if it is possible to split a string from the middle which will give 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 12 min read Check if both halves of the string have at least one different character Earlier we have discussed on how to check if both halves of the string have same set of characters. Now, we further extend our problem on checking if both halves of the string have at least one different character. Examples: Input : baaaabOutput: No, both halves do not differ at allThe two halves co 15 min read Python - Check if String Contain Only Defined Characters using Regex In this article, we are going to see how to check whether the given string contains only a certain set of characters in Python. These defined characters will be represented using sets. Examples: Input: â657â let us say regular expression contains the following characters- (â78653â) Output: Valid Exp 2 min read Python Set | Check whether a given string is Heterogram or not Given a string S of lowercase characters. The task is to check whether a given string is a Heterogram or not using Python. A heterogram is a word, phrase, or sentence in which no letter of the alphabet occurs more than once. Input1: S = "the big dwarf only jumps"Output1: YesExplanation: Each alphabe 3 min read Python | Check if frequencies of all characters of a string are different Given a string S consisting only of lowercase letters, the task is to check if the frequency of all characters of the string is unique. Examples: Input : abaccc Output : Yes âaâ occurs two times, âbâ occurs once and âcâ occurs three times. Input : aabbc Output : No Frequency of both 'a' and 'b' are 3 min read Python | Count the Number of matching characters in a pair of string The problem is about finding how many characters are the same in two strings. We compare the strings and count the common characters between them. In this article, we'll look at different ways to solve this problem.Using Set Sets are collections of unique items, so by converting both strings into se 2 min read Python program to check if a string contains all unique characters To implement an algorithm to determine if a string contains all unique characters. Examples: Input : s = "abcd" Output: True "abcd" doesn't contain any duplicates. Hence the output is True. Input : s = "abbd" Output: False "abbd" contains duplicates. Hence the output is False. One solution is to cre 3 min read Check if String formed by first and last X characters of a String is a Palindrome Given a string str and an integer X. The task is to find whether the first X characters of both string str and reversed string str are same or not. If it is equal then print true, otherwise print false. Examples: Input: str = abcdefba, X = 2Output: trueExplanation: First 2 characters of both string 5 min read Check if a given string is binary string or not - Python The task of checking whether a given string is a binary string in Python involves verifying that the string contains only the characters '0' and '1'. A binary string is one that is composed solely of these two digits and no other characters are allowed. For example, the string "101010" is a valid bi 3 min read Quick way to check if all the characters of a string are same Given a string, check if all the characters of the string are the same or not. Examples: Input : s = "geeks"Output : No Input : s = "gggg" Output : Yes Recommended PracticeCheck StringTry It! Simple Way To find whether a string has all the same characters. Traverse the whole string from index 1 and 8 min read Like