Given n strings, find the common characters in all the strings. In simple words, find characters that appear in all the strings and display them in alphabetical order or lexicographical order.
Note* we’ll be considering that the strings contain lower case letters only.
Input : geeksforgeeks gemkstones acknowledges aguelikes Output : e g k s Input : apple orange Output : a e
We’ll use two hash arrays of size 26 (for a-z, where 0 is a, and z is 25).
The approach will be simple, if we have seen a character before we’ll mark it and if we haven’t then ignore the character because it is not a common one.
commonCharacters : for i= 0 to n-1: // here m is length of ith string for j = 0 to m-1: if ( character seen before ) : mark the character else : ignore it display all the marked characters
e g k s
This article is contributed by Shubham Rana. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Interleaving of two given strings with no common characters
- Print common characters of two Strings in alphabetical order
- Python code to print common characters of two Strings in alphabetical order
- Count common characters in two strings
- Number of common base strings for two strings
- Strings formed from given characters without any consecutive repeating characters
- LCS (Longest Common Subsequence) of three strings
- Check if two strings have a common substring
- Longest common anagram subsequence from N strings
- Count common subsequence in two strings
- Check if there is any common character in two given strings
- Count the number of common divisors of the given strings
- Program to find Greatest Common Divisor (GCD) of N strings
- Longest Common Substring in an Array of Strings
- Construct an Array of Strings having Longest Common Prefix specified by the given Array
- Pair of strings having longest common prefix of maximum length in given array
- Length of longest common prefix possible by rearranging strings in a given array
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters