forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vinay4.py
31 lines (23 loc) · 763 Bytes
/
vinay4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)