我编写了一个程序来获取字符串中匹配相邻字母的计数 [英] I wrote a program to get the count of matching adjacent alphabets in a string

查看:39
本文介绍了我编写了一个程序来获取字符串中匹配相邻字母的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于相同的 1 个测试用例已经通过,而其他所有测试用例都失败了.失败的测试用例是非常长的字符串.但不明白我哪里出错了.

For the same 1 of the test cases have passed while all the other had failed. The failed ones test cases were of very long strings. but could not understand where did I go wrong.

在主函数中读取测试用例的数量和字符串,并将字符串传递给该函数.

The number of test cases and string is been read in the main function, and string gets passed to this function.

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 个测试用例.其中,一个是珠宝=abcddcbd";另一个是abcd".第一个字符串的预期输出为 3,第二个字符串的预期输出为 0.但是,我得到了这个测试用例的预期输出.但所有其他的都失败了(那些是很长的字符串)

for the passed 1, there were 2 test cases. In that, one being jewels="abcddcbd" and the other being "abcd". Expected Output was 3 for the first string and 0 for the second. however, i got the expected output for this test case. but failed all other ones(those are very long strings)

jewels="edmamjboxwzfjsgnmycuutvkhzerdiabcvzlnoazreuavyemxqwgyzdvrzyohamwamziqvdduequyyspfipvigooyqmwllvp"

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

Can somebody help me in knowing what is wrong in my code or how can I obtain the result I want?

提前致谢!!!

推荐答案

听起来很Jewel Quest 拼图.检查字符串中是否有相邻的相等字符,将它们移除并将计数器加 1.然后从头再来,直到不再.

Sounds a Jewel Quest puzzle. Checking a string for adjacent equal characters, remove them and increase the counter by 1. Removing the two characters from the string could produce a new one with adjacent equal characters so it must be checked again from the beginning to remove them, increase the counter, and do it all over again until no more.

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.

Writes 3, 0, and 5 respectively.

这篇关于我编写了一个程序来获取字符串中匹配相邻字母的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆