📜  查找两个字符串不常见的字符|套装2

📅  最后修改于: 2021-04-27 22:13:57             🧑  作者: Mango

给定两个字符串str1str2 ,任务是查找并打印两个给定字符串的不常见字符,并按排序顺序进行,而无需使用额外的空间。此处不常见的字符表示该字符出现在一个字符串,或者出现在另一个字符串,但都不出现在两个字符串中。字符串仅包含小写字符,并且可以包含重复项。

例子:

方法:这里讨论了使用哈希的方法。也可以使用位操作解决此问题。
该方法使用2个变量,这些变量存储每个字符的ASCII码的左移1的按位或– 97,即’a’为0,’b’为1,依此类推。对于这两个字符串,在执行这些按位运算后,我们都会得到一个整数。现在,这两个整数的异或运算将仅在表示罕见字符的位置将二进制位设为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


输出:
fioqruz