给定两个字符串str1和str2 ,任务是在不使用额外空间的情况下按排序顺序查找和打印两个给定字符串的不常见字符。此处,不常见字符意味着该字符要么出现在一个字符串,要么出现在另一个字符串但不同时出现在这两个字符串中。字符串仅包含小写字符并且可以包含重复项。
例子:
Input: str1 = “characters”, str2 = “alphabets”
Output: b c l p r
Input: str1 = “geeksforgeeks”, str2 = “geeksquiz”
Output: f i o q r u z
方法:这里讨论了一种使用散列的方法。这个问题也可以使用位操作来解决。
该方法使用 2 个变量来存储左移 1 与每个字符的 ASCII 代码的按位 OR – 97,即 0 代表 ‘a’,1 代表 ‘b’,依此类推。对于这两个字符串,我们在执行这些按位运算后得到一个整数。现在,这两个整数的 XOR 将仅在表示不常见字符的那些位置处将二进制位设为 1。打印这些位置的字符值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
输出:
fioqruz
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。