如何在字符串中列出特定频率的字符

本文关键字:频率 字符 字符串 | 更新日期: 2023-09-27 17:56:34

场景是例如一个字符串"这是一个测试"现在对于例如路径长度设置为= 2(它可能因我们而异),结果应该是"I",因为这是唯一重复两次的字符串。如果路径长度设置为 = 3,那么结果应该是 (T,S,空格) 请帮助我。

如何在字符串中列出特定频率的字符

string item = "THIS IS A TEST ";
List<char> result = item.GroupBy(x => x).Where(y => y.Count() == 2).Select(y => y.Key).ToList();

这将返回一个字符列表,出现两次(I

我将给定字符串中的所有字符分组,并将其外观计数与2进行比较

string content = "THIS IS A TEST ";
content.GroupBy(c => c).Where(c => c.Count() == 3).Select(c => c.Key);

执行此操作的一种有效方法是创建从重复次数到重复多次的字符列表的查找:

var content = "this is a test";
var lookup = content
    .GroupBy(ch => ch)
    .ToLookup(grp => grp.Count, grp => grp.Key);
var twiceRepeatingChars = lookup[2];
var thriceRepeatingChars = lookup[3];
// and so on
这是我

编码的冗长的解决方案:)没有分组依据。

public static void GetNumberOfTimesACharacterOccuredInAString(string text, int number)
{
        Dictionary<char, int> CharactorNumberOfOccurence = new Dictionary<char, int>();
        int count = 1;
        foreach (char c in text.ToLower())
        {
            if (CharactorNumberOfOccurence.Any(a => a.Key.Equals(c)))
            {
                count = CharactorNumberOfOccurence.Where(a => a.Key.Equals(c)).Select(a => a.Value).FirstOrDefault() + 1;
                CharactorNumberOfOccurence.Remove(c);
                CharactorNumberOfOccurence.Add(c, count);
            }
            else
            {
                CharactorNumberOfOccurence.Add(c, count);
            }
        }
        foreach (KeyValuePair<char, int> charactor in CharactorNumberOfOccurence)
        {
            if (charactor.Value.Equals(number))
            {
                if (char.IsWhiteSpace(charactor.Key))
                    Console.WriteLine("SPACE");
                else
                    Console.WriteLine(charactor.Key.ToString().ToUpper());
            }
        }
}

通过分组非常简单:)

public static void GetNumberOfTimesACharacterOccuredInAString(string text, int number)
{
            List<char> result = text.GroupBy(x => x).Where(y => y.Count() == number).Select(y => y.Key).ToList();
            foreach (char c in result)
            {
                if (char.IsWhiteSpace(c))
                {
                    Console.WriteLine("SPACE");
                }
                else
                {
                    Console.WriteLine(c.ToString());
                }
            }
 }
        string[] arrpattern = new string[textBox.Text.Length];
        int[] arrcount = new int[textBox.Text.Length];
        int counter=0;
        bool found;
        string pattern="";
        for (int i = 0; i < textBox.Text.Length-Convert.ToInt32(textBox2.Text)+1; i++)
        {
            pattern=textBox.Text.Substring(i,Convert.ToInt32(textBox2.Text));
            if(counter==0)
            {
                arrpattern[0] = pattern;
                arrcount[0] = 1;
                counter++;
            }
            else
            {
                found = false;
                for(int j=0; j<counter;j++)
                {
                    if(arrpattern[j]==pattern)
                    {
                        arrcount[j] = arrcount[j] + 1;
                        found = true;
                        break;
                    }
                }
                if(found==false)
                {
                    arrpattern[counter] = pattern;
                    arrcount[counter] = 1;
                    counter++;
                }
            }
        }
        int max=0;
        for(int i =0; i<counter;i++)
        {
            if(arrcount[i]>max)
            {
                max = arrcount[i];
            }
        }
        textBox3.Text=max.ToString();
        textBox1.Text="";
        for (int i = 0; i < counter; i++)
        {
            if (arrcount[i] == max)
            {
                textBox1.Text += "," + arrpattern[i];
            }
        }
    }

}