Please read the new rule regarding the restriction on the use of AI tools. ×

pzwnt's blog

By pzwnt, history, 4 years ago, In English

Hello, I'm looking for an explanation about why my code gives wrong output in the platform. Unfortunately google platform does not provide the input test set, so I'm clueless. I've locally tested it with random input and they all seem right to me; I'd appreciate anyone explaining or providing an input that breaks my impl. and outputs wrong answer. Thanks advance!

This is C# imple.; basically in first iteration I keep the indexes of 'START' substrings and then counting the pairs on the second loop.

private static int solve(string text)
    {
        if (text == string.Empty)
            return 0;

        string s1 = "KICK";
        string s2 = "START";
        List<int> indexes = new List<int>();
        int cnt = 0;
        int start_s1 = 0;
        for (int i = 0; i < text.Length; i++)
        {
            if((i + s1.Length) > text.Length)
            {
                break;
            }

            if (text.Substring(i, 4).Equals(s1))
            {
                start_s1 = i;
                for (int j = i + 4; j < text.Length; j++)
                {
                    if((j + s2.Length) > text.Length)
                    {
                        break;
                    }
                    if (text.Substring(j, 5).Equals(s2))
                    {
                        indexes.Add(j);
                        cnt++;
                    }                        
                }
                break;
            }              
        }

        for (int i = start_s1 + 4;  i < text.Length; i++)
        {
            if (i + s1.Length > text.Length)
                break;

            if (text.Substring(i, 4).Equals(s1))
            {
                for (int j = 0; j < indexes.Count; j++)
                {
                    if (i > indexes[j])
                    { continue; }
                    else 
                    { cnt += indexes.Count - j; }
                    break;                        
                }
            }
        }
        return cnt;
    }
  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?