Posts

[01 Feb 2020] Python code to print common characters of two Strings in alphabetical order

Python code to print common characters of two Strings in alphabetical order 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), 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 u...