本文概述
给定两个字符串str1和str2, 任务是查找并打印两个给定字符串中不常见的字符, 并且不使用多余的空间。此处不常见的字符表示该字符出现在一个字符串中, 或者出现在另一个字符串中, 但都不出现在两个字符串中。字符串仅包含小写字符, 并且可以包含重复项。
例子:
输入:str1 ="characters", str2 ="alphabets"
输出:b c l p r
输入:str1 ="geeksforgeeks", str2 ="geeksquiz"
输出:f i o q r u z
方法:这里讨论了一种使用散列的方法。这个问题也可以通过使用位操作来解决。
该方法使用2个变量, 分别存储每个字符的ASCII码左移1的按位或-97, 即0表示" a", 1表示" b", 依此类推。对于这两个字符串, 在执行这些按位运算后, 我们都会得到一个整数。现在, 这两个整数的异或运算将仅在表示罕见字符的位置将二进制位设为1。打印这些位置的字符值。
下面是上述方法的实现:
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 = "lsbin" ;
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 = "lsbin" ;
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 = "lsbin" ;
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 = "lsbin" ;
str2 = "geeksquiz" ;
printUncommon(str1, str2);
# This code is contributed by AnkitRai01
输出如下:
fioqruz