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



Sample input:
4
RRGB


Sample output:
1

Submission status: Accepted

Solution code:
import time
start = time.time()
import sys
# sys.stdin = open('input.txt')

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 = s.count('R') * s.count('G') * s.count('B')
# print(ans)

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

print(ans)
# end = time.time()
# print(end - start)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
S = list(s)
r = S.count('R')
g = S.count('G')
b = S.count('B')
ans = r * g * b
for k in range(n):
        for j in range(k):
            i = 2*j - k
            if i >= 0 and i < j and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                ans -= 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
sum = 0
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
sum += r*g*b

for i in range(n):
    wi = (n-i+1)//2
    for w in range(1,wi):
        if s[i] == s[i+w] or s[i+w] == s[i+2*w] or s[i] == s[i+2*w]:
            continue
        else:
            sum -= 1

print(sum)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

cnt = Counter(S)

ans = cnt['R'] * cnt['G'] * cnt['B']

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
if N>3 :
  total = S.count("R")*S.count("G")*S.count("B")
  for i in range(0,N-2):
    for j in range(i+1,N-1):
      if 2*j-i<N:
        if S[i]!=S[j] and S[j]!=S[2*j-i] and S[2*j-i]!=S[i]:
          total-=1
  print(total)
else:
  print(0)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

n = int(input())
s = list(input())

rnum = s.count('R')
gnum = s.count('G')
bnum = s.count('B')
ans = rnum * gnum * bnum
for i in range(n):
    for j in range(n):
        d = i+j 
        f = d+j 
        if f>=n:
            break
        if s[i]!=s[d] and s[d]!=s[f] and s[f]!=s[i]:
            ans -= 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections

n = int(input())
s = input()
res = 0
# for i in range(0, n):
#     for j in range(i+1,n):
#         if s[i] != s[j]:
#             for k in range(j+1,n):
#                 if j-i!=k-j and s[i] != s[k] and s[j] != s[k]:
#                     res += 1
#
# print(res)
num = collections.Counter(s)
# print(num)
num_r = num["R"]
num_g = num["G"]
num_b = n - num_r - num_g
count_2 = 0
for x in range(1,int(n/2)+1):
    for i in range(n-2*x):
        # print(x)
        # print(i)
        # print()
        if (i + 2 * x < n and s[i] != s[i + x] and s[i + x] != s[i + 2 * x] and s[i] != s[i + 2 * x] ):
            count_2 += 1
            # print(x)
            # print(i)
            # print()

res = num_r * num_g * num_b - count_2
# print(num_r)
# print(num_g)
# print(num_b)
print(res)

Submission status: Accepted

Solution code:
N = int(input())
s = input()


# for i in range(N-2):
#     for j in range(i+1, N-1):
#         if s[i] == s[j]:
#             continue
#         for k in range(j+1, N):
#             if j-i == k-j:
#                 continue
#             if s[i] == s[k] or s[j] == s[k]:
#                 continue
#             cnt += 1

r = 0
g = 0
b = 0

for t in s:
    if t == "R":
        r += 1
    elif t == "G":
        g += 1

b = N - r - g
cnt = r * g * b

for i in range(1, N-1):
    j = 1
    while 0 <= i-j and i+j <= N-1:
        if s[i] != s[i-j] and s[i] != s[i+j] and s[i-j] != s[i+j]:
            cnt -= 1
        j += 1

print(cnt)

Submission status: Accepted

Solution code:
#!python3

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


def main():
    ans = S.count("R") * S.count("G") * S.count("B")
    
    lim = (N - 1) // 2 + 1
    for i in range(1, lim):
        lim2 = N - 2 * i
        for j in range(lim2):
            if S[j] != S[j+i] and S[j+i] != S[j+2*i] and S[j] != S[j+2*i]:
                ans -= 1
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
c = 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:
            continue
        if s[i] == s[j] or s[j] == s[k] or s[k] == s[i]:
            continue
        c -= 1
print(c)

Submission status: Accepted

Solution code:
n=int(input())
ln=input()
r=set()
g=set()
b=set()
for i,alp in enumerate(ln):
    if alp=="R":
        r.add(i)
    elif alp=="G":
        g.add(i)
    elif alp=="B":
        b.add(i)
max=len(r)*len(g)*len(b)
cnt=0

for i in range(n):
    for j in range(i+1,n):
        k=j+j-i
        if k<n:
            if ln[i]==ln[j] or ln[j]==ln[k] or ln[i]==ln[k]:
                continue
            else:
                cnt+=1

max-=cnt
print(max)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue
from decimal import *


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def divisor(n):
        res = []
        i = 1
        for i in range(1, int(n ** 0.5) + 1):
            if n % i == 0:
                res.append(i)
                if i != n // i:
                    res.append(n // i)
        return res

    @staticmethod
    def round_up(a, b):
        return -(-a // b)

    @staticmethod
    def is_prime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


def pop_count(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    N = Scanner.int()
    S = Scanner.string()
    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 = 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)


def main():
    # sys.stdin = open("sample.txt")
    solve()


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter


def solve():
    n = int(input())
    s = input()
    cnt = Counter(s)
    ans = cnt['R'] * cnt['G'] * cnt['B']
    for i in range(n):
        for j in range(i + 1, n):
            k = j + j - i
            if k < n:
                if s[i] == s[j] or s[i] == s[k] or s[j] == s[k]:
                    continue
                ans -= 1
    return ans


print(solve())

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
arr_R = []
arr_G = []
arr_B = []

for idx,s in enumerate(S):
  if s=="R":
    arr_R.append(idx+1)
  elif s=="G":
    arr_G.append(idx+1)
  else:
    arr_B.append(idx+1)

ans = len(arr_R)*len(arr_G)*len(arr_B)
omit = 0

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

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    s = input()
    cnt = [0] * 3
    for i in range(n):
        cnt[ord(s[i]) % 3] += 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 s[i] == s[j] or s[i] == s[k] or s[j] == s[k]:
                    continue
                ans -= 1
    return ans


print(solve())

Submission status: Accepted

Solution code:
def solve():
    n = int(input())

    # ord('B') == 66 (== 0 mod 3)
    # ord('R') == 82 (== 1 mod 3)
    # ord('G') == 71 (== 2 mod 3)
    s = [ord(c) % 3 for c in input()]
    ans = 1
    ans = s.count(0) * s.count(1) * s.count(2)
    for i in range(n):
        for j in range(i + 1, n):
            k = j + j - i
            if k < n:
                if s[i] == s[j] or s[i] == s[k] or s[j] == s[k]:
                    continue
                ans -= 1
    return ans


print(solve())

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
#from itertools import combinations # (string,3) 3回
#from collections import deque
#import collections.defaultdict(list)
#import bisect
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    # j - i ≠ k - j という式から、 j - i = k - j を満たす奴を減らせばいーのよ

    n = int(input())
    S = input()
    cnt = S.count('R')*S.count('G')*S.count('B') # 全通り
    # N が 4000 O(n^2)なら間に合うか！
    # k = 2j - i という式？　短縮できそう？
    for i in range(n):
        for j in range(i+1,n):# 同じところを参照しても意味ないので、iの次から調べてみる
            if S[i] == S[j]:# まずiとj(隣り合うもの)が一緒はだめ
                continue
            k = 2*j - i # = を満たしてしまうものをひく
            if k >= n or S[k] == S[i] or S[k] == S[j]: # n 以上(ありえない)の場合
            # それと、記号が一致はしてないよね？ って時は、数え上げてすらないからだめ
            # continueする
                continue
            cnt -= 1
    print(cnt)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
n = int(input())
s = 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+(n-i-1)//2+1):
        sj = s[j]
        k = 2*j-i
        if k < n and s[k] != si and s[k] != sj and si != sj:
            ans -= 1

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
class BIT:

    def __init__(self, x):
        """
        Args:
            x (list)
        """
        len_array = len(x)
        self.tree = [0] * (len_array + 1)
        
        # update BIT
        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():
    # count first
    # calc the combinations
    # create three BIT and count the num
    # if S[0] == 'R', S[1] == 'G', then I have to count the number of 'B' in S[3:].
    # O(4000 * 4000 * log4000) = O(10^8). it's gonna be a realistic choice.
    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()

Submission status: Accepted

Solution code:
def main():
    # N <= 4000
    # if full search, O(4000^3) = O(10^10). it's not realistic choice.
    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)
        
        # update BIT
        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():
    # count first
    # calc the combinations
    # create three BIT and count the num
    # if S[0] == 'R', S[1] == 'G', then I have to count the number of 'B' in S[3:].
    # O(4000 * 4000 * log4000) = O(10^8). it's gonna be a realistic choice.
    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()

Submission status: Accepted

Solution code:
#D
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
# print(r, g, b)
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)

Submission status: Accepted

Solution code:
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

# k-j==j-iの場合を引く
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:

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()

Submission status: Accepted

Solution code:

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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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 # k - j = j - i
            if s[i] == s[k]:continue
            if s[j] == s[k]:continue
            count -= 1

print(count)

Submission status: Accepted

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

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

counter = R * G * B 
#j-i != k-j
# 1 2 3 ,1 3 5

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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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]
# print(a)
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]:
                # print(i,j,k,a[i],a[j],a[k])
                ans -=1



# print(cnt)
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)