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

时间:2015-06-17 06:46:17

标签: c#

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

5 个答案:

答案 0 :(得分:13)

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进行比较

答案 1 :(得分:2)

string content = "THIS IS A TEST ";

content.GroupBy(c => c).Where(c => c.Count() == 3).Select(c => c.Key);

答案 2 :(得分:2)

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

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

答案 3 :(得分:1)

这是我编写的一个冗长的解决方案:)没有分组。

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

使用group by非常简单:)

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

答案 4 :(得分:0)

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


}