该函数接受字符串输入并删除出现一定次数的所有字符

时间:2018-07-30 11:41:20

标签: c# asp.net string function dictionary

我有一个单词“ angoora”,如果用户输入为2,则“ a”和“ o”出现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;
}

4 个答案:

答案 0 :(得分:3)

您可以使用LINQs 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)));
}

答案 1 :(得分:2)

您可以使用此功能

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;
}

答案 2 :(得分:0)

您的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;
}

答案 3 :(得分:-1)

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()出现每个唯一字符,如果大于或等于给定数字,则将其删除。