Find uncommon characters of the two strings using Bit Manipulation (original) (raw)
Last Updated : 11 Jul, 2025
Given two strings **s1 and **s2, the task is to **find the **uncommon **characters in **both strings. An uncommon character means that the **character is **present in only **one string or in **another string but **not in **both. The strings contain only **lowercase characters and can have **duplicates.
**Note: Output the uncommon characters in **sorted order.
**Examples:
**Input: s1 = “geeksforgeeks”, s2 = “geeksquiz”
**Output: “fioqruz”
**Explanation: The characters ‘f’, ‘i’, ‘o’, ‘q’, ‘r’, ‘u’, and ‘z’ are present in either s1 or s2, but not in both.**Input: s1 = “characters”, s2 = “alphabets”
**Output: “bclpr”
**Explanation: The characters ‘b’, ‘c’, ‘l’, ‘p’, and ‘r’ are present in either s1 or s2, but not in both.**Input: s1 = “rome”, s2 = “more”
**Output: “”
**Explanation: Both strings contain the same characters, so there are no unique characters. The output is an empty string.
**Note: We have already discussed the naive approach using nested loops and an optimized approach using hash table to solve this problem in Find uncommon characters of the two strings.
**Approach:
The idea is to use **two variables that store the **bitwise **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 bitwise operations and **bitwise XOR of these two integers will give the binary **bit as **1 at only those **positions that denote **uncommon characters because bitwise XOR of **opposite **bit is **1. Print the character **values for those **positions.
Below is the implementation of the above approach:
C++ `
// C++ implementation to find the uncommon // characters of the two strings #include <bits/stdc++.h> using namespace std;
// function to find the uncommon characters // of the two strings string uncommonChars(string &s1, string &s2) {
int a1 = 0, a2 = 0;
for (int i = 0; i < s1.length(); i++) {
// Converting character to ASCII code
int ch = int(s1[i]) - 'a';
// Bit operation
a1 = a1 | (1 << ch);
}
for (int i = 0; i < s2.length(); i++) {
// Converting character to ASCII code
int ch = int(s2[i]) - 'a';
// Bit operation
a2 = a2 | (1 << ch);
}
// XOR operation leaves only uncommon
// characters in the res variable
int res = a1 ^ a2;
// to store the resultant string
string ans = "";
int i = 0;
while (i < 26) {
if (res % 2 == 1) {
ans.push_back(char('a' + i));
}
res = res / 2;
i++;
}
return ans;}
int main() { string s1 = "characters"; string s2 = "alphabets"; cout<<uncommonChars(s1, s2); return 0; }
Java
// Java implementation to find the uncommon // characters of the two strings import java.util.Arrays;
class GfG {
// function to find the uncommon characters
// of the two strings
static String uncommonChars(String s1, String s2) {
int a1 = 0, a2 = 0;
for (int i = 0; i < s1.length(); i++){
// Converting character to ASCII code
int ch = (s1.charAt(i)) - 'a';
// Bit operation
a1 = a1 | (1 << ch);
}
for (int i = 0; i < s2.length(); i++) {
// Converting character to ASCII code
int ch = (s2.charAt(i)) - 'a';
// Bit operation
a2 = a2 | (1 << ch);
}
// XOR operation leaves only uncommon
// characters in the res variable
int res = a1 ^ a2;
// to store the resultant string.
StringBuilder ans = new StringBuilder();
int i = 0;
while (i < 26) {
if (res % 2 == 1) {
ans.append((char) (i + 'a'));
}
res = res / 2;
i++;
}
return ans.toString();
}
public static void main(String[] args) {
String s1 = "characters";
String s2 = "alphabets";
System.out.println(uncommonChars(s1, s2));
}}
Python
Python implementation to find the uncommon
characters of the two strings
function to find the uncommon characters
of the two strings
def uncommonChars(s1, s2) :
a1 = 0; a2 = 0;
for i in range(len(s1)) :
# Converting character to ASCII code
ch = ord(s1[i]) - ord('a');
# Bit operation
a1 = a1 | (1 << ch);
for i in range(len(s2)) :
# Converting character to ASCII code
ch = ord(s2[i]) - ord('a');
# Bit operation
a2 = a2 | (1 << ch);
# XOR operation leaves only uncommon
# characters in the res variable
res = a1 ^ a2;
# to store the resultant string
ans = ""
i = 0;
while (i < 26) :
if (res % 2 == 1) :
ans = "".join([ans,chr(i + ord('a'))])
res = res // 2;
i += 1;
return ansif name == "main": s1 = "characters" s2 = "alphabets" print(uncommonChars(s1, s2))
C#
// C# implementation to find the uncommon // characters of the two strings using System; using System.Text;
class GfG {
// function to find the uncommon characters
// of the two strings
static string uncommonChars(string s1, string s2) {
int a1 = 0, a2 = 0;
for (int i = 0; i < s1.Length; i++) {
// Converting character to ASCII code
int ch = (s1[i] - 'a');
// Bit operation
a1 = a1 | (1 << ch);
}
for (int i = 0; i < s2.Length; i++) {
// Converting character to ASCII code
int ch = (s2[i] - 'a');
// Bit operation
a2 = a2 | (1 << ch);
}
// XOR operation leaves only uncommon
// characters in the res variable
int res = a1 ^ a2;
// to store the resultant string.
StringBuilder ans = new StringBuilder("");
int j = 0;
while (j < 26) {
if (res % 2 == 1) {
ans.Append((char)(j + 'a'));
}
res = res / 2;
j++;
}
return ans.ToString();
}
static void Main(string[] args) {
string s1 = "characters";
string s2 = "alphabets";
Console.WriteLine(uncommonChars(s1, s2));
}}
JavaScript
// Javascript implementation to find the uncommon // characters of the two strings
function uncommonChars(s1, s2) {
let a1 = 0, a2 = 0;
for (let i = 0; i < s1.length; i++) {
// Converting character to ASCII code
let ch
= (s1[i].charCodeAt(0)) - "a".charCodeAt(0);
// Bit operation
a1 = a1 | (1 << ch);
}
for (let i = 0; i < s2.length; i++) {
// Converting character to ASCII code
let ch
= (s2[i].charCodeAt(0)) - "a".charCodeAt(0);
// Bit operation
a2 = a2 | (1 << ch);
}
// XOR operation leaves only uncommon
// characters in the res variable
let res = a1 ^ a2;
// to store the resultant string.
let ans = "";
let i = 0;
while (i < 26) {
if (res % 2 == 1) {
ans = ans.concat(
String.fromCharCode(i + "a".charCodeAt(0)));
}
res = parseInt(res / 2);
i++;
}
return ans;}
//driver code let s1 = "characters"; let s2 = "alphabets"; console.log(uncommonChars(s1, s2));
`
**Time Complexity: O(m + n), where **m and **n are the sizes of the two strings respectively.
**Auxiliary Space: O(1), no any other extra space is required, so it is a constant.