查找两个字符串之间的不常见字符

Find uncommon characters between two strings

我有以下代码:

public static void Main (string[] args) {
    string word1 = "AN";
    string word2 = "ANN";

    //First try:
    var intersect = word1.Intersect(word2); 
    var unCommon1 = word1.Except(intersect).Union(word2.Except(intersect));

    //Second try:
    var unCommon = word1.Except(word2).Union(word2.Except(word1));              
  }

我试图得到的结果是 N。我尝试了几种通过阅读在线帖子来获取它的方法,但我无法弄清楚。有没有办法使用 linq 在两个字符串之间获取不常见的字符。

字符串中字符的顺序无关紧要。 这里还有几个场景: FOO & BAR 将产生 F、O、O、B、A、R。 ANN & NAN 将生成空字符串。

这是一个简单的 LINQ 函数。

string word1 = "AN";
string word2 = "ANN";

//get all the characters in both strings
var group = string.Concat(word1, word2)

    //remove duplicates
    .Distinct()

    //count the times each character appears in word1 and word2, find the
    //difference, and repeat the character difference times
    .SelectMany(i => Enumerable.Repeat(i, Math.Abs(
        word1.Count(j => j == i) - 
        word2.Count(j => j == i))));