本文概述
- C ++
- Java
- C#
- Python3
例子:
输入: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。打印这些位置的字符值。
【查找两个字符串中不常见的字符|S2】下面是上述方法的实现:
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
推荐阅读
- 算法题(最长回文子串的长度)
- C++ STL中的multiset多集cbegin()和cend()函数
- Python GUI Tkinter入门用法指南
- 并发控制技术详细介绍
- Win8.1如何给“这台电脑”改名?
- Win8每次开机都要2次如何处理?
- Win8找不到本地安全策略的处理办法
- Windows8系统商店提示未连接到Internet的处理办法
- Win8如何设置ReadyBoost?Win8设置ReadyBoost的办法