Python code to print common characters of two Strings in alphabetical order Last Updated : 25 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two strings, print all the common characters in lexicographical order. If there are no common letters, print -1. All letters are lower case. Examples: Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e(2 times), g(1 time), k(1 time) and s(1 time). Hence the lexicographical output is "eegks" Input : string1 : hhhhhello string2 : gfghhmh Output : hhh This problem has existing solution please refer Print common characters of two Strings in alphabetical order link. We will solve this problem in python using intersection property and collections.Counter() module. Approach is simple, Convert both strings into dictionary data type using Counter(str) method, which contains characters of string as key and their frequencies as value.Now find common elements between two strings using intersection ( a&b ) property.Resultant will also be an counter dictionary having common elements as keys and their common frequencies as value.Use elements() method of counter dictionary to expand list of keys by their frequency number of times.Sort the list and concatenate each character of output list without space to print resultant string. Implementation: Python3 # Function to print common characters of two Strings # in alphabetical order from collections import Counter def common(str1,str2): # convert both strings into counter dictionary dict1 = Counter(str1) dict2 = Counter(str2) # take intersection of these dictionaries commonDict = dict1 & dict2 if len(commonDict) == 0: print (-1) return # get a list of common elements commonChars = list(commonDict.elements()) # sort list in ascending order to print resultant # string on alphabetical order commonChars = sorted(commonChars) # join characters without space to produce # resultant string print (''.join(commonChars)) # Driver program if __name__ == "__main__": str1 = 'geeks' str2 = 'forgeeks' common(str1, str2) Output: eegks Time complexity : O(n) Auxiliary Space : O(n) Comment More infoAdvertise with us Next Article Python code to print common characters of two Strings in alphabetical order S Shashank Mishra Follow Improve Article Tags : Python python-string Python string-programs Practice Tags : python Similar Reads Python | Check order of character in string using OrderedDict( ) Given an input string and a pattern, check if characters in the input string follows the same order as determined by characters present in the pattern. Assume there wonât be any duplicate characters in the pattern. Examples: Input: string = "engineers rock"pattern = "er";Output: trueExplanation: All 3 min read Find all duplicate characters in string in Python In this article, we will explore various methods to find all duplicate characters in string. The simplest approach is by using a loop with dictionary.Using Loop with DictionaryWe can use a for loop to find duplicate characters efficiently. First we count the occurrences of each character by iteratin 2 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 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 Convert a List of Characters into a String - Python Our task is to convert a list of characters into a single string. For example, if the input is ['H', 'e', 'l', 'l', 'o'], the output should be "Hello".Using join() We can convert a list of characters into a string using join() method, this method concatenates the list elements (which should be strin 2 min read Like