📌  相关文章
📜  查找给定字符串中不存在于其他字符串中的所有字符

📅  最后修改于: 2022-05-13 01:56:07.710000             🧑  作者: Mango

查找给定字符串中不存在于其他字符串中的所有字符

给定两个字符串str1str2 ,它们的长度为NM 。第二个字符串包含第一个字符串的所有字符,但也有一些额外的字符。任务是找出第二个字符串中出现的所有额外字符。

例子:

方法:这个问题的解决方案是基于散列的概念。请按照以下步骤操作:

  • 创建一个大小为 26的空哈希表并递增第一个字符串(str1)的每个字符的所有出现。
  • 现在遍历第二个字符串(str2)删除第一个字符串的所有字符以及在哈希表中添加新字符
  • 哈希表中剩余的字符是多余的字符。

下面是上述方法的实现

C++
// C++ program to implement the approach
#include 
using namespace std;
 
// Function to find the extra characters
vector findTheDifference(string str1,
                               string str2)
{
    vector vs(26, 0);
    vector ans;
 
    // Loop to find the frequency
    // of characters in str1
    for (int i = 0; i < str1.size(); i++)
        vs[str1[i] - 'a']++;
 
    // Adjusting frequency in str2
    for (int i = 0; i < str2.size(); i++) {
 
        // If character was present in str1
        if (vs[str2[i] - 'a'] > 0)
            vs[str2[i] - 'a']--;
        else
            vs[str2[i] - 'a']++;
    }
 
    // Loop to find the extra characters
    for (int i = 0; i < 26; i++)
        if (vs[i] > 0)
            ans.push_back(i + 'a');
    return ans;
}
 
// Driver code
int main()
{
    // Given string
    string str1 = "abcd";
    string str2 = "dabcdehi";
 
    // Function call
    vector ans = findTheDifference(str1, str2);
 
    for (char c : ans)
        cout << c << " ";
    return 0;
}


Java
// Java program to implement the approach
import java.util.*;
class GFG {
 
  // Function to find the extra characters
  public static ArrayList
    findTheDifference(String str1, String str2)
  {
    ArrayList vs = new ArrayList<>();
    for (int i = 0; i < 26; i++)
      vs.add(0);
 
    ArrayList ans = new ArrayList<>();
 
    // Loop to find the frequency
    // of characters in str1
    for (int i = 0; i < str1.length(); i++)
      vs.set(str1.charAt(i) - 'a',
             vs.get(str1.charAt(i) - 'a') + 1);
 
    // Adjusting frequency in str2
    for (int i = 0; i < str2.length(); i++) {
 
      // If character was present in str1
      if (vs.get(str2.charAt(i) - 'a') > 0)
        vs.set(str2.charAt(i) - 'a',
               vs.get(str2.charAt(i) - 'a') - 1);
      else
        vs.set(str2.charAt(i) - 'a',
               vs.get(str2.charAt(i) - 'a') + 1);
    }
 
    // Loop to find the extra characters
    for (int i = 0; i < 26; i++)
      if (vs.get(i) > 0) {
 
        ans.add((char)(i + 97));
      }
 
    return ans;
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    // Given string
    String str1 = "abcd";
    String str2 = "dabcdehi";
 
    // Function call
    ArrayList ans
      = findTheDifference(str1, str2);
 
    for (int i = 0; i < ans.size(); i++) {
 
      System.out.print((ans.get(i)) + " ");
    }
  }
}
 
// This code is contributed by Palak Gupta


Python3
# python3 program to implement the approach
 
# Function to find the extra characters
def findTheDifference(str1, str2):
 
    vs = [0 for _ in range(26)]
    ans = []
 
    # Loop to find the frequency
    # of characters in str1
    for i in range(0, len(str1)):
        vs[ord(str1[i]) - ord('a')] += 1
 
    # Adjusting frequency in str2
    for i in range(0, len(str2)):
 
        # If character was present in str1
        if (vs[ord(str2[i]) - ord('a')] > 0):
            vs[ord(str2[i]) - ord('a')] -= 1
        else:
            vs[ord(str2[i]) - ord('a')] += 1
 
    # Loop to find the extra characters
    for i in range(0, 26):
        if (vs[i] > 0):
            ans.append(chr(i + ord('a')))
    return ans
 
# Driver code
if __name__ == "__main__":
 
    # Given string
    str1 = "abcd"
    str2 = "dabcdehi"
 
    # Function call
    ans = findTheDifference(str1, str2)
 
    for c in ans:
        print(c, end=" ")
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to find the extra characters
  static char[] findTheDifference(string str1,
                                  string str2)
  {
    char[] vs = new char[26];
    for (int i = 0; i < 26; i++)
    {
      vs[i] = '0';
    }
 
    char[] ans = new char[1000];
 
    // Loop to find the frequency
    // of characters in str1
    for (int i = 0; i < str1.Length; i++)
      vs[str1[i] - 'a']++;
 
    // Adjusting frequency in str2
    for (int i = 0; i < str2.Length; i++) {
 
      // If character was present in str1
      if (vs[str2[i] - 'a'] > 0)
        vs[str2[i] - 'a']--;
      else
        vs[str2[i] - 'a']++;
    }
 
    // Loop to find the extra characters
    for (int i = 0; i < 26; i++)
      if (vs[i] > 0)
        string.Concat(ans, i + 'a');
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
    // Given string
    string str1 = "abcd";
    string str2 = "dabcdehi";
 
    // Function call
    char[] ans = findTheDifference(str1, str2);
 
    foreach (char c in ans)
      Console.Write(c + " ");
  }
}
 
// This code is contributed by sanjoy_62.


Javascript



输出
d e h i 

时间复杂度: O(M)
辅助空间: O(1)。