Find uncommon characters of the two strings | Set 2
Last Updated :
25 Nov, 2021
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++
#include <bits/stdc++.h>
using namespace std;
void printUncommon(string str1, string str2)
{
int a1 = 0, a2 = 0;
for ( int i = 0; i < str1.length(); i++) {
int ch = int (str1[i]) - 'a' ;
a1 = a1 | (1 << ch);
}
for ( int i = 0; i < str2.length(); i++) {
int ch = int (str2[i]) - 'a' ;
a2 = a2 | (1 << ch);
}
int ans = a1 ^ a2;
int i = 0;
while (i < 26) {
if (ans % 2 == 1) {
cout << char ( 'a' + i);
}
ans = ans / 2;
i++;
}
}
int main()
{
string str1 = "geeksforgeeks" ;
string str2 = "geeksquiz" ;
printUncommon(str1, str2);
return 0;
}
|
Java
class GFG
{
static void printUncommon(String str1, String str2)
{
int a1 = 0 , a2 = 0 ;
for ( int i = 0 ; i < str1.length(); i++)
{
int ch = (str1.charAt(i)) - 'a' ;
a1 = a1 | ( 1 << ch);
}
for ( int i = 0 ; i < str2.length(); i++)
{
int ch = (str2.charAt(i)) - 'a' ;
a2 = a2 | ( 1 << ch);
}
int ans = a1 ^ a2;
int i = 0 ;
while (i < 26 )
{
if (ans % 2 == 1 )
{
System.out.print(( char ) ( 'a' + i));
}
ans = ans / 2 ;
i++;
}
}
public static void main(String[] args)
{
String str1 = "geeksforgeeks" ;
String str2 = "geeksquiz" ;
printUncommon(str1, str2);
}
}
|
C#
using System;
class GFG
{
static void printUncommon( string str1, string str2)
{
int a1 = 0, a2 = 0;
for ( int i = 0; i < str1.Length; i++)
{
int ch = (str1[i] - 'a' );
a1 = a1 | (1 << ch);
}
for ( int i = 0; i < str2.Length; i++)
{
int ch = (str2[i] - 'a' );
a2 = a2 | (1 << ch);
}
int ans = a1 ^ a2;
int j = 0;
while (j < 26)
{
if (ans % 2 == 1)
{
Console.Write(( char )( 'a' + j));
}
ans = ans / 2;
j++;
}
}
public static void Main()
{
string str1 = "geeksforgeeks" ;
string str2 = "geeksquiz" ;
printUncommon(str1, str2);
}
}
|
Python3
def printUncommon(str1, str2) :
a1 = 0 ; a2 = 0 ;
for i in range ( len (str1)) :
ch = ord (str1[i]) - ord ( 'a' );
a1 = a1 | ( 1 << ch);
for i in range ( len (str2)) :
ch = ord (str2[i]) - ord ( 'a' );
a2 = a2 | ( 1 << ch);
ans = a1 ^ a2;
i = 0 ;
while (i < 26 ) :
if (ans % 2 = = 1 ) :
print ( chr ( ord ( 'a' ) + i),end = "");
ans = ans / / 2 ;
i + = 1 ;
if __name__ = = "__main__" :
str1 = "geeksforgeeks" ;
str2 = "geeksquiz" ;
printUncommon(str1, str2);
|
Javascript
<script>
function printUncommon(str1, str2)
{
var a1 = 0, a2 = 0;
for ( var i = 0; i < str1.length; i++) {
var ch = (str1[i].charCodeAt(0)) - 'a' .charCodeAt(0);
a1 = a1 | (1 << ch);
}
for ( var i = 0; i < str2.length; i++) {
var ch = (str2[i].charCodeAt(0)) - 'a' .charCodeAt(0);
a2 = a2 | (1 << ch);
}
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++;
}
}
var str1 = "geeksforgeeks" ;
var str2 = "geeksquiz" ;
printUncommon(str1, str2);
</script>
|
Time Complexity: O(|str1| + |str2| + 26)Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...