Special String Again

  • + 15 comments

    The hardest part, was getting my brain to refocus on this "Special Palindrone" definition. Originally I was coding for a normal palindrone, which is VASTLY different and more complicated. Once I wasted time on that and re-read the problem, the solution wasn't too difficult.

    My Solution: C#

        static long substrCount(string s)
        {
            long retVal = s.Length;
    
            for (int i = 0; i < s.Length; i++)
            {
                var startChar = s[i];
                int diffCharIdx = -1;
                for (int j = i + 1; j < s.Length; j++)
                {
                    var currChar = s[j];
                    if (startChar == currChar)
                    {
                        if ((diffCharIdx == -1) ||
                           (j - diffCharIdx) == (diffCharIdx - i))
                            retVal++;
                    }
                    else
                    {
                        if (diffCharIdx == -1)
                            diffCharIdx = j;
                        else
                            break;
                    }
                }
            }
            return retVal;
        }