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.
- Remove characters from the first string which are present in the second string
- A Program to check if strings are rotations of each other or not
- Print list items containing all characters of a given word
- Find the smallest window in a string containing all characters of another string
- Longest Common Subsequence | DP-4
- Length of the longest substring without repeating characters
- Check whether two strings are anagram of each other
- Print all permutations with repetition of characters
- Print all interleavings of given two strings
- Interleaving of two given strings with no common characters
- String matching where one string contains wildcard characters
- Longest Common Substring | DP-29
- Find if a string is interleaved of two other strings | DP-33
- Find the first non-repeating character from a stream of characters
- Rearrange a string so that all same characters become d distance away