Python counter and dictionary intersection example (Make a string using deletion and rearrangement) Last Updated : 21 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Given two strings, find if we can make first string from second by deleting some characters from second and rearranging remaining characters. Examples: Input : s1 = ABHISHEKsinGH : s2 = gfhfBHkooIHnfndSHEKsiAnG Output : Possible Input : s1 = Hello : s2 = dnaKfhelddf Output : Not Possible Input : s1 = GeeksforGeeks : s2 = rteksfoGrdsskGeggehes Output : Possible We have existing solution for this problem please refer Make a string from another by deletion and rearrangement of characters link. We will this problem quickly in python. Approach is very simple, Convert both string into dictionary using Counter(iterable) method, each dictionary contains characters within string as Key and their frequencies as Value.Now take intersection of two dictionaries and compare resultant output with dictionary of first string, if both are equal that means it is possible to convert string otherwise not. Implementation: Python3 # Python code to find if we can make first string # from second by deleting some characters from # second and rearranging remaining characters. from collections import Counter def makeString(str1,str2): # convert both strings into dictionaries # output will be like str1="aabbcc", # dict1={'a':2,'b':2,'c':2} # str2 = 'abbbcc', dict2={'a':1,'b':3,'c':2} dict1 = Counter(str1) dict2 = Counter(str2) # take intersection of two dictionaries # output will be result = {'a':1,'b':2,'c':2} result = dict1 & dict2 # compare resultant dictionary with first # dictionary comparison first compares keys # and then compares their corresponding values return result == dict1 # Driver program if __name__ == "__main__": str1 = 'ABHISHEKsinGH' str2 = 'gfhfBHkooIHnfndSHEKsiAnG' if (makeString(str1,str2)==True): print("Possible") else: print("Not Possible") OutputPossible Time Complexity: O(n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Python counter and dictionary intersection example (Make a string using deletion and rearrangement) S Shashank Mishra Follow Improve Article Tags : Strings Python DSA python-dict python-string Python dictionary-programs +2 More Practice Tags : pythonpython-dictStrings Similar Reads Replacing Characters in a String Using Dictionary in Python In Python, we can replace characters in a string dynamically based on a dictionary. Each key in the dictionary represents the character to be replaced, and its value specifies the replacement. For example, given the string "hello world" and a dictionary {'h': 'H', 'o': 'O'}, the output would be "Hel 2 min read Python - Converting list string to dictionary Converting a list string to a dictionary in Python involves mapping elements from the list to key-value pairs. A common approach is pairing consecutive elements, where one element becomes the key and the next becomes the value. This results in a dictionary where each pair is represented as a key-val 3 min read Using Counter() in Python to find minimum character removal to make two strings anagram Given two strings in lowercase, the task is to make them Anagram. The only allowed operation is to remove a character from any string. Find minimum number of characters to be deleted to make both the strings anagram? If two strings contains same data set in any order then strings are called Anagrams 3 min read How to replace words in a string using a dictionary mapping In Python, we often need to replace words in a string based on a dictionary mapping, where keys are words to replace and values are their replacements. For example, given the string "the quick brown fox jumps over the lazy dog" and the dictionary {"quick": "slow", "lazy": "active"}, we want to repla 4 min read Generate Two Output Strings Depending upon Occurrence of Character in Input String - Python The task of generating two output strings based on the occurrence of characters in an input string in Python involves classifying characters based on their frequency. We need to create one string that contains characters that appear only once in the input string and another string for characters tha 4 min read Like