我编写了一个程序来获取字符串中匹配的相邻字母的计数

I wrote a program to get the count of matching adjacent alphabets in a string

对于相同的 1 个测试用例已通过,而其他所有测试用例均未通过。失败的测试用例是很长的字符串。但不明白我哪里错了。

主函数中读取了测试用例数和字符串,并将字符串传递给该函数。

public static int getMaxScore(string jewels)
{
    string temp=jewels;
    int count=0;
    for(int i=0;i<temp.Length-1;i++)
    {
        for(int j=i;j<temp.Length-1;j++)
        {
            if(jewels[i]==jewels[j+1])
            {
                temp=jewels.Remove(i,2);
                count++;                      
            }
            else
            {
                continue;
            }    
        }                
    }        
    return count;
}

对于通过的1,有2个测试用例。其中,一个是 jewels="abcddcbd",另一个是 "abcd"。第一个字符串的预期输出为 3,第二个字符串为 0。但是,我得到了这个测试用例的预期输出。但所有其他的都失败了(那些是很长的字符串)

jewels="edmamjboxwzfjsgnmycuutvkhzerdiabcvzlnoazreuavyemxqwgyzdvrzyohamwamziqvdduequyyspfipvigooyqmwllvp"

谁能帮我知道我的代码有什么问题,或者我怎样才能得到我想要的结果?

提前致谢!!!

        public static int CountThings(string s)
        {
            if(s.Length < 2) { return 0; }

            int n = 0;

            for (int i = 0; i < s.Length - 1; i++)
            {
                if (s[i] == s[i + 1])
                {
                    int start = i;
                    int end = i + 1;
                    while (s[start] == s[end] && start >= 0 && end <= s.Length - 1)
                    {
                        n++;
                        start--;
                        end++;
                    }
                }
            }
            return n;
        }

听起来是个 Jewel Quest 难题。检查字符串中相邻的相等字符,删除它们并将计数器加 1。从字符串中删除两个字符可能会产生一个具有相邻相等字符的新字符,因此必须从头开始再次检查以删除它们,增加计数器,并从头再来,直到不再。

public static int getMaxScore(string jewels)
{
    var count = 0;
    var max = jewels.Length;
    var i = 0;
    var chars = jewels.ToList();
    var adj = 2; //<- Change to increase the number of adjacent chars.

    while (i < max)
    {
        if (chars.Count >= adj && i <= chars.Count - adj &&
            chars.Skip(i).Take(adj).Distinct().Count() == 1)
        {
            count++;
            chars.RemoveRange(i, adj);
            max = chars.Count;
            i = 0;
        }
        else
            i++;
    }

    return count;
}

测试:

void TheCaller()
{
    Console.WriteLine(getMaxScore("abcddcbd"));
    Console.WriteLine(getMaxScore("abcd"));
    Console.WriteLine(getMaxScore("edmamjboxwzfjsgnmycuutvkhzerdiabcvzlnoazreuavyemxqwgyzdvrzyohamwamziqvdduequyyspfipvigooyqmwllvp"));
}

分别写入3、0、5。

对于沙砾和小腿,这是一个紧凑的递归版本:

static void Main(string[] args)
{
    string jewels = "edmamjboxwzfjsgnmycuutvkhzerdiabcvzlnoazreuavyemxqwgyzdvrzyohamwamziqvdduequyyspfipvigooyqmwllvp";
    int score = getMaxScore(new StringBuilder(jewels));

    Console.WriteLine($"jewels = {jewels}");
    Console.WriteLine($"score = {score}");

    Console.Write("Press Enter to Quit.");
    Console.ReadLine();
}

static int getMaxScore(StringBuilder jewels)
{
    for(int i=0; i<(jewels.Length-1); i++)
        if (jewels[i]==jewels[i+1])
            return 1 + getMaxScore(jewels.Remove(i, 2));
    return 0;
}