Problem:
 Score : 400 points    Problem Statement We have a string S of length N consisting of R , G , and B .  Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:   S_i \neq S_j , S_i \neq S_k , and S_j \neq S_k .  j - i \neq k - j .       Constraints  1 \leq N \leq 4000  S is a string of length N consisting of R , G , and B .         Input Input is given from Standard Input in the following format:  N  S       Output Print the number of triplets in question.        Sample Input 1 4
RRGB      Sample Output 1 1  Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.       Sample Input 2 39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB      Sample Output 2 1800

Solution (Python):
class BIT:

    def __init__(self, x):
        """
        Args:
            x (list)
        """
        len_array = len(x)
        self.tree = [0] * (len_array + 1)
        
        
        for i, _x in enumerate(x, 1):
            while i <= len_array:
                self.tree[i] += _x
                i += i&-i

    def __call__(self, i, j):
        """
        return the sum from i to j including i and j.
        """
        ret = self.cumsum(j) - self.cumsum(i-1)
        return ret

    def cumsum(self, i):
        """
        cumulative sum from 1 to i.
        """
        ret = 0
        while i > 0:
            ret += self.tree[i]
            i -= i&-i
        return ret


def main():
    
    
    
    
    
    N = int(input())
    S = input()

    BIT_R = [0] * N
    BIT_G = [0] * N
    BIT_B = [0] * N
    for i, s in enumerate(S):
        if s == 'R':
            BIT_R[i] = 1
        elif s == 'G':
            BIT_G[i] = 1
        else:
            BIT_B[i] = 1
    BIT_R = BIT(BIT_R)
    BIT_G = BIT(BIT_G)
    BIT_B = BIT(BIT_B)

    count = 0
    for i in range(N):
        first = S[i]
        for j in range(i+1, N):
            second = S[j]
            if first == second:
                continue
            else:
                exclude_i = 2 * j - i
                if {first, second} == {'R', 'G'}:
                    _count = BIT_B(j+1, N)
                    if (exclude_i < N 
                        and S[exclude_i] == 'B'):
                        _count -= 1
                    
                elif {first, second} == {'R', 'B'}:
                    _count = BIT_G(j+1, N)
                    if (exclude_i < N 
                        and S[exclude_i] == 'G'):
                        _count -= 1

                else:
                    _count = BIT_R(j+1, N)
                    if (exclude_i < N 
                        and S[exclude_i] == 'R'):
                        _count -= 1
                
                count += _count

    print(count)


if __name__=='__main__':
    main()

Solution (Python):
def main():
    
    
    N = int(input())
    S = input()

    count = 0
    for i in range(N):
        first = S[i]
        for j in range(i+1, N):
            second = S[j]
            if first == second:
                continue
            else:
                for k in range(j+1, N):
                    third = S[k]
                    if (first == third
                        or second == third):
                            continue
                    else:
                        if j - i == k - j:
                            continue
                        else:
                            count += 1

    print(count)


class BIT:

    def __init__(self, x):
        """
        Args:
            x (list)
        """
        len_array = len(x)
        self.tree = [0] * (len_array + 1)
        
        
        for i, _x in enumerate(x, 1):
            while i <= len_array:
                self.tree[i] += _x
                i += i&-i

    def __call__(self, i, j):
        """
        return the sum from i to j including i and j.
        """
        ret = self.cumsum(j) - self.cumsum(i-1)
        return ret

    def cumsum(self, i):
        """
        cumulative sum from 1 to i.
        """
        ret = 0
        while i > 0:
            ret += self.tree[i]
            i -= i&-i
        return ret


def main():
    
    
    
    
    
    N = int(input())
    S = input()

    BIT_R = [0] * N
    BIT_G = [0] * N
    BIT_B = [0] * N
    for i, s in enumerate(S):
        if s == 'R':
            BIT_R[i] = 1
        elif s == 'G':
            BIT_G[i] = 1
        else:
            BIT_B[i] = 1
    BIT_R = BIT(BIT_R)
    BIT_G = BIT(BIT_G)
    BIT_B = BIT(BIT_B)

    count = 0
    for i in range(N):
        first = S[i]
        for j in range(i+1, N):
            second = S[j]
            if first == second:
                continue
            else:
                exclude_i = 2 * j - i
                if {first, second} == {'R', 'G'}:
                    _count = BIT_B(j+1, N)
                    if (exclude_i < N 
                        and S[exclude_i] == 'B'):
                        _count -= 1
                    
                elif {first, second} == {'R', 'B'}:
                    _count = BIT_G(j+1, N)
                    if (exclude_i < N 
                        and S[exclude_i] == 'G'):
                        _count -= 1

                else:
                    _count = BIT_R(j+1, N)
                    if (exclude_i < N 
                        and S[exclude_i] == 'R'):
                        _count -= 1
                
                count += _count

    print(count)


if __name__=='__main__':
    main()

Solution (Python):

n = int(input())
s = list(input())
r = g = b = 0
for i in s:
    if i == "R":
        r += 1
    elif i == "G":
        g += 1
    elif i == "B":
        b += 1

ans = r * g * b
for i in range(n - 2):
    si = s[i]
    for j in range(i + 1, i + 1 +((n - 1 - i) // 2)):
        if si != s[j]:
            sj = s[j]
            k = 2 * j - i
            if si != s[k] and sj != s[k]:
                ans -= 1
print(ans)

Solution (Python):
N = int(input())
S = list(input())

r_num = S.count("R")
g_num = S.count("G")
b_num = S.count("B")

count = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = j * 2 - i
        if k >= N:
            continue
        si, sj, sk = S[i], S[j], S[k]
        if si != sj and sj != sk and sk != si:
            count += 1

print(r_num * g_num * b_num - count)

Solution (Python):
n=int(input())
s=input()

r=0
g=0
b=0
count=0

for i in s:
  if i == "R":
    r += 1
  elif i == "G":
    g += 1
  elif i == "B":
    b += 1

count = r*g*b


for i in range(n-2):
  si = s[i]
  for j in range(i+1,i+(n-i-1)//2+1):
    if si != s[j]:
      sj = s[j]
      k = j+(j-i)
      if s[k] != si and s[k] != sj:
        count -= 1
        
print(count)

Solution (Python):
n=int(input())
s=input()

r=0
g=0
b=0
count=0

for i in s:
  if i == "R":
    r += 1
  elif i == "G":
    g += 1
  elif i == "B":
    b += 1

count = r*g*b


for i in range(n-2):
  si = s[i]
  for j in range(i+1,i+(n-i-1)//2+1):
    if si != s[j]:
      sj = s[j]
      k = j+(j-i)
      if s[k] != si and s[k] != sj:
        count -= 1
        
print(count)

Solution (Python):
n=int(input())
s=input()
r=0
g=0
b=0
for i in s:
    if i=="R":r+=1
    elif i=="G":g+=1
    elif i=="B":b+=1
ans=r*b*g

for j in range(n):
    for i in range(j):
        k=j+(j-i)
        if k<n:
            
            if s[i]==s[j]: continue
            if s[j]==s[k]: continue
            if s[k]==s[i]: continue
            ans-=1
print(ans)

Solution (Python):
N = int(input())
S = str(input())
R = 0
G = 0
B = 0
Z = 0
for i in range(N):
    if S[i] == 'R':
        R += 1
    if S[i] == 'G':
        G += 1
    if S[i] == 'B':
        B += 1

for i in range(N-1):
    for j in range(i+1, N):
        k = 2 * j - i
        if k > N-1:
            continue
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            Z += 1
print(R*G*B-Z)

Solution (Python):
import sys

def main():
    def input(): return sys.stdin.readline().rstrip()
    
    N = int(input())
    S = input()
    
    r, g, b = S.count('R'), S.count('G'), S.count('B')
    
    exce = 0
    for i in range(N):
        for j in range(i, N):
            k = 2*j - i
            if k < len(S):
                if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                    exce += 1
    print(r*g*b - exce)
    
        
    
if __name__ == '__main__':
    main()

Solution (Python):
import sys

def main():

    
    N = int(input())
    S = input()
    
    r, g, b = S.count('R'), S.count('G'), S.count('B')
    
    exce = 0
    for i in range(N):
        for j in range(i, N):
            k = 2*j - i
            if k < len(S):
                if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                    exce += 1
    print(r*g*b - exce)
    
        
    
if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = input()
r = 0
g = 0
b = 0
for i in range(n):
    if s[i] == 'R':
        r += 1
    elif s[i] == 'G':
        g += 1
    else:
        b += 1
ans = r*g*b
for i in range(n-2):
    for j in range(i+1,n-1):
        k = 2*j - i
        if k < n:
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i] :
                ans -= 1
print(ans)

Solution (Python):
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 6)


def input(): return sys.stdin.readline().rstrip()


N = int(input())
S = input()
counter = Counter(S)
ans = counter['R'] * counter['G'] * counter['B']

for i in range(N):
    for j in range(i + 1, N):
        k = 2 * j - i
        if k < N:
            if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
R = s.count("R")
G = s.count("G")
B = s.count("B")
cnt = 0
for i in range(n-1):
    for j in range(i+1,n):
        if j - i <= n - 1 - j:
            if s[i] != s[j] and s[j] != s[2*j - i] and s[i] != s[2*j - i]:
                cnt += 1
print(R*G*B - cnt)

Solution (Python):
from collections import Counter
n=int(input())
s=input()
r=g=b=0
for i in range(len(s)):
    if s[i]=='R':r+=1
    elif s[i]=='G':g+=1
sum=r*g*(n-r-g)

for i in range(len(s)):
    r=i
    for j in range(1,len(s)):
        g=i+j
        b=i+j+j
        if g<len(s) and b<len(s):
            if s[r]!=s[b] and s[g]!=s[b] and s[r]!=s[g]:sum-=1
print(sum)

Solution (Python):
N = int(input())
S = input()

total_comb = S.count('R')*S.count('G')*S.count('B')

minus = 0
for i in range(N-2):
    for j in range(i+1, N):
        k = 2*j - i
        if k < N:
            if (S[i] != S[j]) and (S[j] != S[k]) and (S[k] != S[i]):
                minus += 1

print(total_comb - minus)

Solution (Python):
n=int(input())
s=input()
a=s.count("R")
b=s.count("G")
c=s.count("B")
sum=0
for i in range(n-2):
  for j in range(i+1,n-1):
    if 2*j-i<=n-1 and s[i]!=s[j] and s[j]!=s[2*j-i] and s[2*j-i]!=s[i]:
      sum=sum+1
print(a*b*c-sum)

Solution (Python):

def main():
    n = int(input())
    s = input()
    r,g,b = 0,0,0
    for i in s:
        if i == 'R': r += 1
        elif i == 'G': g += 1
        elif i == 'B': b += 1
    ans = r*g*b
    for j in range(n):
        sj = s[j]
        for i in range(j):
            k = j+(j-i)
            if k < n:
                si = s[i]
                sk = s[k]
                if si == sj or si == sk or sk == sj:
                    continue
                ans -= 1
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):

def main():
    n = int(input())
    s = input()
    r,g,b = 0,0,0
    for i in s:
        if i == 'R': r += 1
        elif i == 'G': g += 1
        elif i == 'B': b += 1
    ans = r*g*b
    for j in range(n):
        for i in range(j):
            k = j+(j-i)
            if k < n:
                if s[i] == s[j] or s[i] == s[k] or s[k] == s[j]:
                    continue
                ans -= 1
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = input()
ans = s.count('R') * s.count('G') * s.count('B')
for j in range(n):
    for i in range(j):
        k = j+(j-i)
        if k < n:
            if s[i] == s[j] or s[i] == s[k] or s[k] == s[j]:
                continue
            ans -= 1

print(ans)

Solution (Python):
def main():
    n = int(input())
    s = input()
    ans = s.count('R') * s.count('G') * s.count('B')
    for j in range(n):
        for i in range(j):
            k = j+(j-i)
            if k < n:
                if s[i] == s[j] or s[i] == s[k] or s[k] == s[j]:
                    continue
                ans -= 1

    print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = input()
firstcond = s.count('R') * s.count('G') * s.count('B')
minus = 0

for k in range(3, n + 1):
    for j in range(2, k):
        i = 2 * j - k
        if 1 <= i < j:
            if (s[i - 1] != s[j - 1]) and (s[j - 1] != s[k - 1]) and (s[k - 1] != s[i - 1]):
                minus += 1

print(firstcond - minus)

Solution (Python):
n = int(input())
s = input()

nr = s.count("R")
ng = s.count("G")
nb = s.count("B")

count = nr * ng * nb

for i in range(n-2):
    for j in range(i+1, (n+i+1)//2):
        if s[i] == s[j]:continue
        else:
            k = 2*j - i 
            if s[i] == s[k]:continue
            if s[j] == s[k]:continue
            count -= 1

print(count)

Solution (Python):
n = int(input())
s = str(input())

R = s.count("R")
G = s.count("G")
B = s.count("B")

counter = R * G * B 



for i in range(n-1):
    for j in range(i+1,(n+i+1)//2):
        if s[i] != s[j] and s[i] != s[2*j-i] and s[2*j -i] != s[j] :
            counter -= 1

print(counter)

Solution (Python):
N=int(input())
S=input()
all=S.count("R")*S.count("G")*S.count("B")

for i in range(N-1):
  for j in range(i,N):
    if 0<=2*i-j<N:
      if S[2*i-j]!=S[i]!=S[j]!=S[2*i-j]:
        all-=1
print(all)

Solution (Python):
n = int(input())
s = input()
a = []
for i in range(n):
    if s[i] == 'R' :
         a.append(0)
    if s[i] == 'G' :
         a.append(1)
    if s[i] == 'B' :
         a.append(2)
cnt = [0,0,0]
for i in range(n):
    cnt[a[i]] +=1
ans = 1

for i in range(3):
    ans *= cnt[i]

for i in range(n):
    for j in range(i+1,n):
        k = j+(j-i)
        if k<n:
            if a[i] != a[j] and  a[i] != a[k]and a[k] != a[j]:
                
                ans -=1




print(ans)

Solution (Python):
N = int(input())
S = str(input())

r = 0
g = 0
b = 0
for s in range(N):
    if S[s] == 'R':
        r += 1
    elif S[s] == 'G':
        g += 1
    elif S[s] == 'B':
        b += 1

ans = (r*g*b)

for i in range(N):
    for k in range(N):
        j = int((i+k)/2)
        if k <= i or (i+k)%2 != 0 or j <= i or k <= j:
            continue
        if (S[i] != S[j]) and (S[j] != S[k]) and (S[i] != S[k]):
            ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = input()
ans = s.count("R")*s.count("G")*s.count("B")


for i in range(n):
    p = s[i]
    for j in range(i,n):
        if s[j] != p:
            q = s[j]
            if 2 * j - i < n:
                if s[2*j-i] != p and s[2*j-i] != q:
                     ans -= 1

print(ans)

Solution (Python):
N = int(input())
s = list(input())
r = s.count("R")
g = s.count("G")
b = s.count("B")
ans = r * g * b
for i in range(N):
    for j in range(i+1,N):
        if len(s) <= 2*j-i:
            break;
        k = s[2*j-i]
        if (k != s[i]) and (k !=s[j]) and (s[i] != s[j]):
            ans -= 1
print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
  N = int(input())
  S = input()

  sum = S.count('R') * S.count('G') * S.count('B')

  for i in range(N-2):
    for j in range(i+1, N-1):
      k = j+(j-i)
      if k < N:
        if S[i] == S[j] or S[i] == S[k] or S[k] == S[j]:
          continue
        sum -= 1
  print(sum)

if __name__ == '__main__':
  main()

Solution (Python):
N = int(input())
S = input()

r = 0
g = 0
b = 0
for s in S:
  if s == "R":
    r += 1
  if s == "G":
    g += 1
  if s == "B":
    b += 1
ans = r * g * b

for i in range(N):
  for j in range(i + 1, N):
    k = 2 * j - i
    if k >= N:
      continue
    if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
      ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
ans = s.count('R')*s.count('B')*s.count('G')
for i in range(n-2):
    for j in range(i+1,n-1):
        k = j + (j - i)
        if k>=n:break
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
            ans -= 1
        
print(ans)

Solution (Python):
from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial
n=int(input())
r=set()
g=set()
b=set()
s=input()
for i in range(len(s)):
	if s[i]=='R':
		r.add(i)
	elif s[i]=='G':
		g.add(i)
	else:
		b.add(i)
count=0
for i in r:
	for j in g:
		x=len(b)
		if 2*j-i in b:
			x-=1
		if 2*i-j in b:
			x-=1
		if (i+j)%2==0 and (i+j)//2 in b:
			x-=1
		count+=x
print(count)

Solution (Python):
n=int(input())
s=input()
count=0
r_list=[]
g_list=[]
b_list=[]
for i in range(len(s)):
    if s[i]=="R":
        r_list.append(i)
    elif s[i]=="G":
        g_list.append(i)
    elif s[i]=="B":
        b_list.append(i)

for i in range(1,len(s)-1):
    if n%2==0:
        if i<n//2:
            for j in range(1,i+1):
                if s[i]!=s[i-j] and s[i]!=s[i+j] and s[i+j]!=s[i-j]:
                    count+=1
        else:
            for j in range(1,n-i):
                if s[i]!=s[i-j] and s[i]!=s[i+j] and s[i+j]!=s[i-j]:
                    count+=1
    else:
            if i<(n-1)//2:
                for j in range(1,i+1):
                    if s[i]!=s[i-j] and s[i]!=s[i+j] and s[i+j]!=s[i-j]:
                        count+=1
            elif i==(n-1)//2:
                for j in range(1,n-i):
                    if s[i]!=s[i-j] and s[i]!=s[i+j] and s[i+j]!=s[i-j]:
                        count+=1
            elif i>(n-1)//2:
                for j in range(1,n-i):
                    if s[i]!=s[i-j] and s[i]!=s[i+j] and s[i+j]!=s[i-j]:
                        count+=1

print(len(r_list)*len(g_list)*len(b_list)-count)

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N = int(readline())
    S = readline().strip()

    
    R = [0]
    G = [0]
    B = [0]
    for i, s in enumerate(S):
        if s == "R":
            R.append(R[-1] + 1)
            G.append(G[-1])
            B.append(B[-1])
        elif s == "G":
            R.append(R[-1])
            G.append(G[-1] + 1)
            B.append(B[-1])
        else:
            R.append(R[-1])
            G.append(G[-1])
            B.append(B[-1] + 1)

    ans = 0
    for i, s in enumerate(S, start=1):
        if s == "R":
            ans += G[i-1] * (B[-1] - B[i])
            ans += B[i-1] * (G[-1] - G[i])
            for j in range(i):
                if S[j] == "G":
                    if 2*i-j-2 < N and S[2*i-j-2] == "B":
                        ans -= 1
                if 2*i-j-2 < N and S[j] == "B":
                    if S[2*i-j-2] == "G":
                        ans -= 1
        
        elif s == "G":
            ans += R[i-1] * (B[-1] - B[i])
            ans += B[i-1] * (R[-1] - R[i])
            for j in range(i):
                if S[j] == "R":
                    if 2*i-j-2 < N and S[2*i-j-2] == "B":
                        ans -= 1
                if 2*i-j-2 < N and S[j] == "B":
                    if S[2*i-j-2] == "R":
                        ans -= 1

        else:
            ans += G[i-1] * (R[-1] - R[i])
            ans += R[i-1] * (G[-1] - G[i])
            for j in range(i):
                if S[j] == "R":
                    if 2*i-j-2 < N and S[2*i-j-2] == "G":
                        ans -= 1
                if S[j] == "G":
                    if 2*i-j-2 < N and S[2*i-j-2] == "R":
                        ans -= 1
    
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
S = input()
ans, gap = S.count('R') * S.count('G') * S.count('B'), 1
while gap * 2 + 1 <= N:
    ans -= sum([1 for i in range(N - gap * 2)
                if {'R', 'G', 'B'} == {S[i], S[i + gap], S[i + 2 * gap]}])
    gap += 1
print(ans)

Solution (Python):
def func1(x, y, z, a):
  if x == y:
    z.append(a)
    return 1 
  return 0

def func2(x, y):
  if x==y:
    return 1
  return 0

def func3(x, y, z, a):
  l = 0
  for i in x:
    for j in y:
      k = 2 * j - i
      if k <= N-1 and i < j:
        l += func2(z[k], a)
  return l

  
N = int(input())
S = input()
r, g, b, rgb, equal = 0, 0, 0, 0, 0
r0, g0, b0 = [], [], []

for i in range(N):
  r += func1(S[i], 'R', r0, i)
  g += func1(S[i], 'G', g0, i)
  b += func1(S[i], 'B', b0, i)

rgb = r * g * b


equal += func3(r0, g0, S, 'B')

equal += func3(r0, b0, S, 'G')

equal += func3(g0, r0, S, 'B')

equal += func3(g0, b0, S, 'R')

equal += func3(b0, r0, S, 'G')

equal += func3(b0, g0, S, 'R')

print(rgb - equal)

Solution (Python):
n = int(input())
s = input()

r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

ans = r_cnt * g_cnt * b_cnt

for i in range(n):      
    for d in range(n):
        j = i + d       
        k = j + d       
        if k >= n:break 
        if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            ans -= 1    

print(ans)

Solution (Python):
n = int(input())
s = input()
 
count = {"R":0,"G":0,"B":0}
for c in s:
    count[c] += 1
 
r,g,b = count.values()
ans = r*g*b
for i in range(1,(n-1)//2+1): 
    for rgb in zip(s,s[i:],s[i*2:]):  
        if len(set(rgb)) == 3:  
            ans -=1
print(ans)

Solution (Python):
N = int(input())
S = input()

r = S.count('R')
g = S.count('G')
b = S.count('B')

count = 0

for i in range(1, N+1):
  for j in range(1, min(i,N-i-1)+1):
    if S[i-j] != S[i] and S[i+j] != S[i] and S[i+j] != S[i-j]:
      count+=1
      
print((r * g * b) - count)

Solution (Python):
n = int(input())
s = str(input())
ans = s.count("R")*s.count("G")*s.count("B")

for i in range(n-2):
    for j in range(i+1, n-1):
        k = int(2*j -i)
        if k <= n-1:
            if s[i] != s[j] and s[k] != s[i] and s[j] != s[k]:
                ans -= 1

print(ans)

Solution (Python):
from collections import defaultdict
d = defaultdict(int)

n = int(input())
s = input()
r=[]
g=[]
b=[]
for i in range(n):
  
  if s[i] == 'R':
    r.append(i)
  elif s[i] == 'G':
    g.append(i)
  else:
    
    d[i] = 1




sum = len(r)*len(g)*len(d)
for i in r:
  for j in g:
    ma = max(i,j)
    mi = min(i,j)
    df = ma - mi
    k1 = mi - df
    k2 = ma + df
    k3 = (ma + mi) / 2
    sum -= d[k1]
    sum -= d[k2]
    sum -= d[k3]
print(sum)

Solution (Python):
num=int(input())
word=input()
alound=word.count("R")*word.count("G")*word.count("B")
for i in range(num-2):
  for j in range(i+1,num-1):
    k=2*j-i
    if k<=num-1:
      if word[i]!=word[j] and word[i]!=word[k] and word[k]!=word[j]:
        alound=alound-1
print(alound)

Solution (Python):
N=int(input())
S=input()
lsR=[]
lsG=[]
lsB=[]

for i in range(N):
  if S[i]=="R":
    lsR.append(i)
  elif S[i]=="G":
    lsG.append(i)
  else:
    lsB.append(i)
a=len(lsR)*len(lsG)*len(lsB)
b=0
for i in range(N):
  for j in range(i+1,N):
    if S[i]!=S[j]:
      k=2*j-i
      if k<N:
        if S[i]!=S[k] and S[j]!=S[k]:
          b+=1
print(a-b)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    s = input()

    r = s.count("R")
    g = s.count("G")
    b = s.count("B")

    res = r * g * b
    for i in range(n):
        for j in range(i, n):
            k = 2 * j - i
            if 0 <= k < n:
                if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                    res -= 1

    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
n = int(input())
s = input()
r = len(list(filter(lambda c: c == 'R', s)))
g = len(list(filter(lambda c: c == 'G', s)))
b = len(list(filter(lambda c: c == 'B', s)))
ans = r * g * b - sum([sum([1 for j in range(i + 1, (n + i + 1) // 2) if s[i] != s[j] and s[j] != s[(k := 2 * j - i)] and s[k] != s[i]]) for i in range(n)])
print(ans)

Solution (Python):
N=int(input())
S=input()
c = 0
rgb = S.count('R')*S.count('G')*S.count('B')
for i in range(N-2):
  for j in range(i+1, N-1):
    if 2*j - i < N:
      if (S[i] != S[j] and S[i] != S[2*j-i] and S[j] != S[2*j-i]):
        c+=1
print(rgb-c)

Solution (Python):
n = int(input())
s = list(input())

cnt = s.count("R")*s.count("G")*s.count("B")
for i in range(0, n):
    for j in range(i+1, n):
        k = 2*j - i
        if s[i] == s[j] or k >= n or s[k] == s[i] or s[k] == s[j]:
            continue
        else:
            cnt -= 1

print(cnt)

Solution (Python):
import re

N = int(input())
S = input()

R = re.findall("R", S)
G = re.findall("G", S)
B = re.findall("B", S)

c = 0
for i in range(N-2):
  for d in range((N-1-i)//2):
    if S[i] != S[i+d+1] and S[i+d+1] != S[i+2*d+2] and S[i+2*d+2] != S[i]:
      c += 1

print(len(R)*len(G)*len(B)-c)

Solution (Python):
n = int(input())
s = input()

result = s.count('R') * s.count('G') * s.count('B')

for fst_idx in range(n):
    for scd_idx in range(fst_idx + 1, n):
        trd_idx = scd_idx * 2 - fst_idx
        if trd_idx >= n:
            break
        elif s[fst_idx] != s[scd_idx] and s[scd_idx] != s[trd_idx] and \
                s[trd_idx] != s[fst_idx]:
            result -= 1

print(result)

Solution (Python):
n=int(input())
s=input()

from collections import Counter

ans = s.count("R") * s.count("G") * s.count("B")

for step in range(1,n//2+2):
    for start in range(n):

        if start+2*step>=n:

            break
        ans-=len(set((s[start], s[start+step],s[start+2*step])))==3
print(ans)

Solution (Python):
from time import time 
N = int(input())
S = str(input())
t0 = time()
x, y, z = 0,0,0
for i in range(N):
    if S[i] == "R":
        x += 1
    elif S[i] == "G":
        y += 1
    elif S[i] == "B":
        z += 1
    else:
        print("ERROR")

no = 0
for s in range(N-2):
    d = 1
    while(s+2*d < N):

        a = S[s]
        b = S[s+d]
        c = S[s+2*d]
        if a != b and b != c and c != a:
            no += 1
        d += 1

print(x*y*z-no)

Solution (Python):
N = int(input())
S = str(input()) 

r = S.count('R')
g = S.count('G')
b = S.count('B')
total = r * g * b

k = 0
count = 0
for i in range(N):
    for j in range(N):
        k = 2 * j - i
        if(i>=j or j >=k or i >=k): continue
        if(i==j or j==k or k==i or k<0 or k>=N): continue
        if(S[i] == S[j] or S[j] == S[k] or S[k] == S[i]): continue
        
        count += 1
        
print(total - count)

Solution (Python):
n = int(input())
s = list(input())
cnt = 0
cnt += s.count('R')*s.count('G')*s.count('B')
for i in range(1,n-1):
    for j in range(1,min(i,n-i-1)+1):
        if s[i-j]!=s[i] and s[i]!=s[i+j] and s[i+j]!=s[i-j]:
            cnt -= 1
print(cnt)

Solution (Python):
import math

k = int(input())
n = input()

r = n.count("R")
g = n.count("G")
b = n.count("B")

ans = r*g*b

for i in range(k):
    
    for j in range(i + 1, math.ceil((k + i)/2)):
        
        if (n[i] != n[j]) and (n[2*j - i] != n[i]) and (n[j] != n[2*j - i]):
            ans -= 1

print(ans)

Solution (Python):
n = int(input())
s = input()

r_cnt = s.count('R')
g_cnt = s.count('G')
b_cnt = s.count('B')

ans = r_cnt * g_cnt * b_cnt

for i in range(n):
    for j in range(1,n+1):
        if i - j < 0 or i + j >= n:
            break
        if s[i-j] != s[i] and s[i] != s[i+j] and s[i+j] != s[i-j]:
            ans -= 1

print(ans)

Solution (Python):

N = int(input())
S = str(input())


import numpy as np
answer = 0
R = np.zeros(N, int)
G = np.zeros(N, int)
B = np.zeros(N, int)
for i in range(N):
    if S[i] == "R":
        R[i] = 1
    elif S[i] == "G":
        G[i] = 1
    elif S[i] == "B":
        B[i] = 1
R = np.cumsum(R)
G = np.cumsum(G)
B = np.cumsum(B)
RR = R[-1]
GG = G[-1]
BB = B[-1]

if N > 3:
    for i in range(1, N-1):
        if S[i] == "R":
            a = G[i]
            b = BB - B[i]
            c = B[i]
            d = GG - G[i]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "R" and S[i+j] != "R" and S[i-j] != S[i+j]:
                    answer -= 1
        elif S[i] == "G":
            a = B[i]
            b = RR - R[i]
            c = R[i]
            d = BB - B[i]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "G" and S[i+j] != "G" and S[i-j] != S[i+j]:
                    answer -= 1
        elif S[i] == "B":
            a = R[i]
            b = GG - G[i]
            c = G[i]
            d = RR - R[i]
            answer += a*b + c*d
            for j in range(1, min(i+1, N-i)):
                if S[i-j] != "B" and S[i+j] != "B" and S[i-j] != S[i+j]:
                    answer -= 1
        
print(answer)

Solution (Python):


import sys














from bisect import bisect_left, bisect_right 




def main():
    mod = 1000000007                  
    inf = float('inf')
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    from itertools import permutations
    n = ii()
    s = input()
    ind_lst = [[] for _ in range(3)]
    d = {'R':0, 'G':1, 'B':2}
    for i, char in enumerate(s):
        ind_lst[d[char]].append(i)
    cnt = 0
    
    
    
    
    
    
    
    
    
    rest_set = set(ind_lst[2])
    num_rest = len(ind_lst[2])
    for x in ind_lst[0]:
        for y in ind_lst[1]:
            decreased = 0
            u, v = min(x, y), max(x, y)
            if 2 * u - v in rest_set:
                decreased += 1
            if (u + v) % 2 == 0 and (u + v) // 2 in rest_set:
                decreased += 1
            if 2 * v - u in rest_set:
                decreased += 1
            cnt += num_rest - decreased
            
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter

N = int(input())
S = str(input())

dic = Counter(S)
ans = dic["R"] * dic["G"] * dic["B"]

for cntr in range(N):
    i = 1
    while True:
        left = cntr - i
        right = cntr + i
        if not (0 <= left and right < N): break
        if S[left] != S[cntr] and S[cntr] != S[right] and S[right] != S[left]: ans -= 1
        i += 1

print(ans)

Solution (Python):
n =int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")

an = 0
for i in range(n-2):
	for j in range(i+1,n-1):
		k = 2*j-i
		if k>=n:
			continue
		else:
			if s[i] != s[j] and s[k] != s[j] and s[k] != s[i]:
				an+=1


print(r*g*b-an)

Solution (Python):

n = int(input())
s = input()
R = []
G = []
B = []
for i in range(n):
    if s[i] == "R":
        R.append(i)
    elif s[i] == "G":
        G.append(i)
    else:
        B.append(i)

ans = len(R) * len(G) * len(B)
for i in range(n):
    for j in range(i + 1, n):
        if 0 <= 2 * j - i and 2 * j - i < n:
            if s[2 * j - i] != s[i] and s[2 * j - i] != s[j] and s[i] != s[j]:
                ans -= 1
print(ans)

Solution (Python):
n = int(input())
s = input()
 
r = s.count('R')
g = s.count('G')
b = s.count('B')
 
ans = r*g*b
 
for i in range(n):
  for j in range(i+2, n, 2):
    if s[i] != s[j] and s[i] != s[int((i+j)/2)] and s[int((i+j)/2)] != s[j]:
      ans -= 1
print(ans)