【问题标题】:function which takes a string input and removes all the characters which occur a certain number of times函数,它接受一个字符串输入并删除所有出现一定次数的字符
【发布时间】:2019-01-06 15:48:33
【问题描述】:

我在这里有一个单词“angoora”,“a”和“o”出现 2 次,如果用户输入为 2,那么输出应该是“ngr”函数应该删除 a 和 o,因为它在字符串中出现 2 次。如果用户输入 3,那么输出应该是“angoora”,因为没有字符出现 3 次。

我正在这样做,但我认为这不是正确的方法,因为它不会引导我实现目标,我们将不胜感激。

public static SortedDictionary<char, int> Count(string stringToCount)
{
    SortedDictionary<char, int> characterCount = new SortedDictionary<char, int>();

    foreach (var character in stringToCount)
    {
        int counter = 0;
        characterCount.TryGetValue(character, out counter);
        characterCount[character] = counter + 1;
    }

    return characterCount;
}

【问题讨论】:

  • "没有把我引向我的目标" 这到底是什么意思?您得到了预期结果,还是得到了错误或意外结果?
  • stringToCount.Where(z =&gt; characterCount[z] &lt; desiredNumber) 可以帮助您入门。

标签: c# asp.net string function dictionary


【解决方案1】:

您的characterCount SortedDictionary 为空。

目前你正在做的事情:

public static SortedDictionary<char, int> Count(string stringToCount)
{
    // Create a new empty SortedDictionary
     SortedDictionary<char, int> characterCount = new SortedDictionary<char, int>();

    // Loop through each character in stringToCount and see if SortedDictionary contains a key equal to this character (it doesn't as dictionary is empty).
    foreach (var character in stringToCount)
     {
         int counter = 0;
         characterCount.TryGetValue(character, out counter);
         characterCount[character] = counter +1;
     }

     return characterCount;
}

你当然想要这样的东西:

public static SortedDictionary<char, int> Count(string stringToCount)
{
    // Create a new empty SortedDictionary (use var keyword if defining variables)
    var characterCount = new SortedDictionary<char, int>();

    // Loop through each character and add to dictionary
    foreach (var character in stringToCount)
    {
        // If character already in SortedDictionary.
        if (characterCount.TryGetValue(character, out int count))
        {
            // Increment count value.
            characterCount[character] = count + 1;
            // Above line can also be: ++characterCount[character];
        }
        // Else, character not already in dictionary.
        else
        {
            // Add character in dictionary and set count to 1.
            characterCount.Add(character, 1);
        }
    }

    return characterCount;
}

【讨论】:

  • 在 mjwills 建议使用 TryGetValue 而不是 ContainsKey 后编辑
  • 这与原始代码有何不同?在我看来基本相同。
  • // Above line can also be: ++characterCount[character]; 不要那样做,否则你会得到另一个哈希查找。
【解决方案2】:
public static string foobar(string given, int number)
{
    string result = given;
    foreach (char c in result.Distinct())
    {
        if (given.Count(ch => c == ch) >= number) result= result.Replace(c.ToString(),"");
    }
    return result;
}

Distinct() 只会给你唯一的字符。 然后你Count()每个唯一字符的出现,如果它大于或等于给定的数字,则删除。

【讨论】:

  • 由于您没有使用ref 传递given,因此当函数返回时,您对它的修改会丢失。一般来说,我不是修改参数的忠实粉丝。我会让函数返回新字符串。
  • @HansKilian 我想展示它是如何工作的,但你是对的。我更新了。
【解决方案3】:

您可以使用 LINQ 的 GroupBy 来查找每个字符出现的次数。然后删除您想要的次数。像这样的

public static string RemoveCharactersThatOccurNumberOfTimes(string s, int numberOfOccurances)
{
    var charactersToBeRemoved = s.GroupBy(c => c).Where(g => g.Count() == numberOfOccurances).Select(g => g.Key);
    return String.Join("", s.Where(c => !charactersToBeRemoved.Contains(c)));
}

【讨论】:

  • 您的解决方案更好,但是如果您将这些代码包装在函数中,那么我认为他会接受您的解决方案,因为他需要函数:)
  • @ershoaib 好建议。谢谢!
【解决方案4】:

你可以使用这个功能

static string Fix(string item, int count)
{
    var chars = item.ToList().GroupBy(g => g).Select(s => new { Ch = s.Key.ToString(), Count = s.Count() }).Where(w => w.Count < count).ToList();
    var characters = string.Join("", item.ToList().Select(s => s.ToString()).Where(wi => chars.Any(a => a.Ch == wi)).ToList());
    return characters;
}

【讨论】:

  • 好吧,我终于找到了解决方案
  • item.ToList() 应该只是 item
猜你喜欢
  • 1970-01-01
  • 2022-11-15
  • 2019-05-03
  • 2022-12-01
  • 1970-01-01
  • 2016-04-30
  • 1970-01-01
  • 2021-02-16
  • 1970-01-01
相关资源
最近更新 更多