Given two strings str1 and str2, the task is to find and print the uncommon characters of the two given strings in sorted order without using extra space. Here uncommon character means that either the character is present in one string or it is present in the 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
Approach: An approach that uses hashing has been discussed here. This problem can also be solved using bit operations.
The approach uses 2 variables that store the bit-wise OR of left shift of 1 with each character’s ascii code – 97 i.e. 0 for ‘a’, 1 for ‘b’ and so on. For both the strings we get an integer after performing these bit-wise operations. Now the XOR of these two integers will give the binary bit as 1 at only those positions that denote uncommon characters. Print the character values for those positions.
Below is the implementation of the above approach:
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
- Concatenated string with uncommon characters of two strings
- Python program to find uncommon words from two Strings
- Concatenated string with uncommon characters in Python
- Shortest Uncommon Subsequence
- Longest 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
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.