Given two strings, print all the common characters in lexicographical order. If there are no common letters, print -1. All letters are lower case.
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 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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Print common characters of two Strings in alphabetical order
- Python - Extract Strings with Successive Alphabets in Alphabetical Order
- Check if the characters of a given string are in alphabetical order
- Print the frequency of each character in Alphabetical order
- Find alphabetical order such that words can be considered sorted
- Reorder the position of the words in alphabetical order
- PyQt5 ComboBox – User entered items store alphabetical order
- Check whether the vowels in a string are in alphabetical order or not
- Sum of the alphabetical values of the characters of a string
- Remove all non-alphabetical characters of a String in Java
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- Interleaving of two given strings with no common characters
- Count common characters in two strings
- Number of common base strings for two strings
- Check whether two strings contain same characters in same order
- Common characters in n strings
- Python | Convert list of strings and characters to list of characters
- Python Program that Displays Letters that are not common in two strings
- Python program to print k characters then skip k characters in a string
- Python | Print all the common elements of two lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.