Problem :

https://www.hackerrank.com/challenges/special-palindrome-again/problem


My Solution :

#!/usr/bin/env python3


def substrCount(s):
    total = 0
    counter = []
    before = s[0]
    count = 1
    for c in s[1:]:
        if c == before:
            count += 1
        else:
            counter.append((before, count))
            before = c
            count = 1
    counter.append((before, count))
    for i in range(len(counter)):
        count = counter[i][1]
        total += count * (count + 1) // 2
        if (i+2 < len(counter) and
            counter[i][0] == counter[i+2][0] and
            counter[i+1][1] == 1):
            total += min(counter[i][1], counter[i+2][1])
    return total


n = int(input())
s = input()
result = substrCount(s)
print(result)