Open In App

Find uncommon characters of the two strings | Set 2

Last Updated : 25 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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, an 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.

Examples:  

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 the 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:  

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the uncommon
// characters in the given string
// in sorted order
void printUncommon(string str1, string str2)
{
    int a1 = 0, a2 = 0;
    for (int i = 0; i < str1.length(); i++) {
 
        // Converting character to ASCII code
        int ch = int(str1[i]) - 'a';
 
        // Bit operation
        a1 = a1 | (1 << ch);
    }
    for (int i = 0; i < str2.length(); i++) {
 
        // Converting character to ASCII code
        int ch = int(str2[i]) - 'a';
 
        // Bit operation
        a2 = a2 | (1 << ch);
    }
 
    // XOR operation leaves only uncommon
    // characters in the ans variable
    int ans = a1 ^ a2;
 
    int i = 0;
    while (i < 26) {
        if (ans % 2 == 1) {
            cout << char('a' + i);
        }
        ans = ans / 2;
        i++;
    }
}
 
// Driver code
int main()
{
    string str1 = "geeksforgeeks";
    string str2 = "geeksquiz";
 
    printUncommon(str1, str2);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
 
    // Function to print the uncommon
    // characters in the given string
    // in sorted order
    static void printUncommon(String str1, String str2)
    {
        int a1 = 0, a2 = 0;
        for (int i = 0; i < str1.length(); i++)
        {
 
            // Converting character to ASCII code
            int ch = (str1.charAt(i)) - 'a';
 
            // Bit operation
            a1 = a1 | (1 << ch);
        }
        for (int i = 0; i < str2.length(); i++)
        {
 
            // Converting character to ASCII code
            int ch = (str2.charAt(i)) - 'a';
 
            // Bit operation
            a2 = a2 | (1 << ch);
        }
 
        // XOR operation leaves only uncommon
        // characters in the ans variable
        int ans = a1 ^ a2;
 
        int i = 0;
        while (i < 26)
        {
            if (ans % 2 == 1)
            {
                System.out.print((char) ('a' + i));
            }
            ans = ans / 2;
            i++;
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str1 = "geeksforgeeks";
        String str2 = "geeksquiz";
 
        printUncommon(str1, str2);
    }
}
 
// This code contributed by Rajput-Ji


C#




// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to print the uncommon
// characters in the given string
// in sorted order
static void printUncommon(string str1, string str2)
{
    int a1 = 0, a2 = 0;
    for (int i = 0; i < str1.Length; i++)
    {
 
        // Converting character to ASCII code
        int ch = (str1[i] - 'a');
 
        // Bit operation
        a1 = a1 | (1 << ch);
    }
    for (int i = 0; i < str2.Length; i++)
    {
 
        // Converting character to ASCII code
        int ch = (str2[i] - 'a');
 
        // Bit operation
        a2 = a2 | (1 << ch);
    }
 
    // XOR operation leaves only uncommon
    // characters in the ans variable
    int ans = a1 ^ a2;
 
    int j = 0;
    while (j < 26)
    {
        if (ans % 2 == 1)
        {
            Console.Write((char)('a' + j));
        }
        ans = ans / 2;
        j++;
    }
}
 
// Driver code
public static void Main()
{
    string str1 = "geeksforgeeks";
    string str2 = "geeksquiz";
 
    printUncommon(str1, str2);
 
}
}
 
// This code is contributed by SoM15242


Python3




# Python3 implementation of the approach
 
# Function to print the uncommon
# characters in the given string
# in sorted order
def printUncommon(str1, str2) :
 
    a1 = 0; a2 = 0;
     
    for i in range(len(str1)) :
 
        # Converting character to ASCII code
        ch = ord(str1[i]) - ord('a');
 
        # Bit operation
        a1 = a1 | (1 << ch);
     
    for i in range(len(str2)) :
 
        # Converting character to ASCII code
        ch = ord(str2[i]) - ord('a');
 
        # Bit operation
        a2 = a2 | (1 << ch);
 
    # XOR operation leaves only uncommon
    # characters in the ans variable
    ans = a1 ^ a2;
 
    i = 0;
    while (i < 26) :
        if (ans % 2 == 1) :
            print(chr(ord('a') + i),end="");
         
        ans = ans // 2;
        i += 1;
 
# Driver code
if __name__ == "__main__" :
 
    str1 = "geeksforgeeks";
    str2 = "geeksquiz";
 
    printUncommon(str1, str2);
     
    # This code is contributed by AnkitRai01


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to print the uncommon
// characters in the given string
// in sorted order
function printUncommon(str1, str2)
{
    var a1 = 0, a2 = 0;
    for (var i = 0; i < str1.length; i++) {
 
        // Converting character to ASCII code
        var ch = (str1[i].charCodeAt(0)) - 'a'.charCodeAt(0);
 
        // Bit operation
        a1 = a1 | (1 << ch);
    }
    for (var i = 0; i < str2.length; i++) {
 
        // Converting character to ASCII code
        var ch = (str2[i].charCodeAt(0)) - 'a'.charCodeAt(0);
 
        // Bit operation
        a2 = a2 | (1 << ch);
    }
 
    // XOR operation leaves only uncommon
    // characters in the ans variable
    var ans = a1 ^ a2;
 
    var i = 0;
    while (i < 26) {
        if (ans % 2 == 1) {
            document.write( String.fromCharCode('a'.charCodeAt(0) + i));
        }
        ans = parseInt(ans / 2);
        i++;
    }
}
 
// Driver code
var str1 = "geeksforgeeks";
var str2 = "geeksquiz";
printUncommon(str1, str2);
 
</script>


Output: 

fioqruz

 

Time Complexity: O(|str1| + |str2| + 26)Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads