0

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

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?

Thanks in Advance!!!

Alexander Schmidt
  • 5,631
  • 4
  • 39
  • 79
Saranya
  • 69
  • 1
  • 4
  • What is the expected output for the input value if jewel? Can you also explain the logic of your code? – Chetan Jul 08 '20 at 12:32
  • `temp=jewels.Remove(i,2);` means `temp` will be shorter than `jewels`. But your loop is `for(int i=0;i `temp.Length`). – mjwills Jul 08 '20 at 12:41
  • `GroupAdjacent` is where I would start. https://morelinq.github.io/2.7/ref/api/html/M_MoreLinq_MoreEnumerable_GroupAdjacent__2_1.htm – mjwills Jul 08 '20 at 12:48
  • Should it start from a? or 'm' 'n' 'o' 'p' are considered adjacent? –  Jul 08 '20 at 13:14
  • A regex like ([a-zA-Z])(\1) detected all the 5 matches of consecutive characters in the jewel string. –  Jul 08 '20 at 13:19
  • For us to help further, what is the expected result for **each** of the following inputs - `abbc` `aabbcc` `aabbaa` `abba` `aaabbbbccccc`? – mjwills Jul 08 '20 at 13:27
  • "In that, one being jewels="abcddcbd" and the other being "abcd". Expected Output was 3 for the first string and 0 for the second." I'm still trying to figure out why the first string would return 3... – Idle_Mind Jul 08 '20 at 14:53
  • @Idle_Mind I suspect the idea is that you remove the adjacent entries, which may then make a _new_ pair adjacent. – mjwills Jul 08 '20 at 15:00
  • @mjwills Gotcha! I see that now. I missed that TWO characters were being removed. – Idle_Mind Jul 08 '20 at 15:05

3 Answers3

0
        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;
        }
Richard Barraclough
  • 2,625
  • 3
  • 36
  • 54
0

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

Testing:

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

Writes 3, 0, and 5 respectively.

0

For grits and shins, here's a compact recursive version:

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;
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40