Find and print the uncommon characters of the two given strings in sorted order. Here uncommon character means that either the character is present in one string or it is present in other string but not in both. The strings contain only lowercase characters and can contain duplicates.
Input: str1 = “characters”, str2 = “alphabets”
Output: b c l p r
Input: str1 = “geeksforgeeks”, str2 = “geeksquiz”
Output: f i o q r u z
Naive Approach: Using two loops, for each character of 1st string check whether it is present in the 2nd string or not. Likewise, for each character of 2nd string check whether it is present in the 1st string or not.
Time Complexity: O(n^2) and extra would be required to handle duplicates.
Efficient Approach: An efficient approach is to use hashing.
- Use a hash table of size 26 for all the lowercase characters.
- Initially, mark presence of each character as ‘0’ (denoting that the character is not present in both the strings).
- Traverse the 1st string and mark presence of each character of 1st string as ‘1’ (denoting 1st string) in the hash table.
- Now, traverse the 2nd string. For each character of 2nd string, check whether its presence in the hash table is ‘1’ or not. If it is ‘1’, then mark its presence as ‘-1’ (denoting that the character is common to both the strings), else mark its presence as ‘2’ (denoting 2nd string).
Below image is a dry run of the above approach:
Below is the implementation of the above approach:
b c l p r
Time Complexity: O(m + n), where m and n are the sizes of the two strings respectively.
This article is contributed by Ayush Jauhari. 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 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.
- Find uncommon characters of the two strings | Set 2
- Concatenated string with uncommon characters of two strings
- Python program to find uncommon words from two Strings
- Concatenated string with uncommon characters in Python
- Longest Uncommon Subsequence
- Shortest Uncommon Subsequence
- Strings formed from given characters without any consecutive repeating characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Interleaving of two given strings with no common characters
- Remove minimum number of characters so that two strings become anagram
- Print common characters of two Strings in alphabetical order
- Least number of manipulations needed to ensure two strings have identical characters
- Python code to print common characters of two Strings in alphabetical order
- Count common characters in two strings
- Compare two strings considering only alphanumeric characters
- Check whether two strings contain same characters in same order
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Minimum characters to be deleted from the beginning of two strings to make them equal
- Minimum characters to be deleted from the end to make given two strings equal
- Check if a string can be split into two strings with same number of K-frequent characters