Aug-05-2020, 12:59 PM
Hey! Friends After Some mind blowing tricks on google i approach with these solution!
# 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 = input('Please Type The First String:\n') str2 = input('Please Type The Second String:\n') common(str1, str2)