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: Time Limit Exceeded

Solution code:
from collections import Counter
n=int(input())
s_=input()
s=Counter(s_)
rgb = s['R'] * s['G'] * s['B']
cnt=0
for i in range(len(s_)):
  for j in range(i+1,len(s_)):
    h=j-i+j
    if h<len(s_):
      if s_[i]!=s_[j] and s_[h]!=s_[j] and s_[i]!=s_[h]:
        cnt+=1

print(rgb-cnt)

Submission status: Time Limit Exceeded

Solution code:

n=int(input())
s_=input()
r, g, b = s_.count("R"), s_.count("G"), s_.count("B")
rgb = r * g * b
cnt=0
for i in range(len(s_)):
  for j in range(i+1,len(s_)):
    h=j-i+j
    if h<=len(s_)-1:
      if s_[i]!=s_[j] and s_[h]!=s_[j] and s_[i]!=s_[h]:
        cnt+=1

print(rgb-cnt)

Submission status: Time Limit Exceeded

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

rgb=['R','G','B']
cnt=0
for i in range(len(s)):
  if s[i]==rgb[0]:
    for j in range(i+1,len(s)):
      if s[j]==rgb[1]:
        for h in range(j+1,len(s)):
          if h-j!=j-i:
            if s[h]==rgb[2]:
              cnt+=1
        
      elif s[j]==rgb[2]:
        for h in range(j+1,len(s)):
          if h-j!=j-i:
            if s[h]==rgb[1]:
              cnt+=1
              
  elif s[i]==rgb[1]:
    for j in range(i+1,len(s)):
      if s[j]==rgb[0]:
        for h in range(j+1,len(s)):
          if h-j!=j-i:
            if s[h]==rgb[2]:
              cnt+=1
        
      elif s[j]==rgb[2]:
        for h in range(j+1,len(s)):
          if h-j!=j-i:
            if s[h]==rgb[0]:
              cnt+=1
              
  elif s[i]==rgb[2]:
    for j in range(i+1,len(s)):
      if s[j]==rgb[1]:
        for h in range(j+1,len(s)):
          if h-j!=j-i:
            if s[h]==rgb[0]:
              cnt+=1
        
      elif s[j]==rgb[0]:
        for h in range(j+1,len(s)):
          if h-j!=j-i:
            if s[h]==rgb[1]:
              cnt+=1
              
print(cnt)

Submission status: Time Limit Exceeded

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

cnt0=len(B)
cnt=0
for i in R:
    for j in G:
        cnt+=cnt0
        koho=set()
        if (j+i)%2==0:
            koho.add((j+i)//2)
        if 0<=2*i-j<N:
            koho.add(2*i-j)
        if 0<=2*j-i<N:
            koho.add(2*j-i)
        for k in koho:
            if k in B:
                cnt-=1
            
                
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
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)
    
cnt=0
for i in R:
    for j in G:
        for k in B:
            mini=min(i,j,k)
            maxi=max(i,j,k)
            if i != mini and i !=maxi:
                mid=i
            if j != mini and j !=maxi:
                mid=j
            if k != mini and k !=maxi:
                mid=k
            if maxi-mid!=mid-mini:
                cnt+=1
                
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools


def make_list(S, N, target):
    target_list = []
    for i in range(N):
        if S[i] == target:
            target_list.append(i+1)
    return target_list


def counting(a_list, b_list, c_list):
    count = 0
    for i in range(len(a_list)):
        for j in range(len(b_list)):
            for k in range(len(c_list)):
                if (b_list[j] - a_list[i] != c_list[k] - b_list[j]) and (a_list[i] <= b_list[j] and b_list[j] <= c_list[k]):
                    count += 1
    return count


if __name__ == "__main__":
    N = int(input())
    S = input()

    R_list = make_list(S, N, 'R')
    G_list = make_list(S, N, 'G')
    B_list = make_list(S, N, 'B')

    p = itertools.permutations([R_list, G_list, B_list], 3)
    count = 0
    for v in p:
        i, j, k = v
        count +=counting(i, j, k)

    print(count)

Submission status: Time Limit Exceeded

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

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
  if s[i] == 'R':
    r.append(i)
  if s[i] == 'G':
    g.append(i)
  if s[i] == 'B':
    b.append(i)
cnt = len(r)*len(g)*len(b)
for i in r:
  for j in g:
    a = abs(i-j)
    B = max(i,j)+a
    c = min(i,j)-a
    if (i+j)%2==0:
      d = (i+j)//2
    else:
      d=-1
    if B in b:
      cnt-=1
    if c in b:
      cnt-=1
    if d in b:
      cnt-=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
from heapq import heappush

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

    C={"R":[],"G":[],"B":[]}
    for i in range(N):
        heappush(C[S[i]],i)

    res=0

    flag1=0
    flag2=0
    for i in C["R"]:
        #j=>G
        if flag1==0:
            start_j=bisect_left(C["G"],i)
            if start_j!=len(C["G"]):
                for g_index in range(start_j,len(C["G"])):
                    j=C["G"][g_index]
                    start_k=bisect_left(C["B"],j)
                    if start_k!=len(C["B"]):
                        for b_index in range(start_k,len(C["B"])):
                            k = C["B"][b_index]
                            if j-i!=k-j:
                                res+=1
                    else:
                        break
            else:
                flag1=1
                if flag2==1:
                    break

        #j=>B
        if flag2==0:
            start_j = bisect_left(C["B"], i)
            if start_j != len(C["B"]):
                for b_index in range(start_j, len(C["B"])):
                    j = C["B"][b_index]
                    start_k = bisect_left(C["G"], j)
                    if start_k != len(C["G"]):
                        for g_index in range(start_k, len(C["G"])):
                            k = C["G"][g_index]
                            if j - i != k - j:
                                res += 1
                    else:
                        break
            else:
                flag2=1
                if flag1==1:
                    break

    flag1=0
    flag2=0

    for i in C["G"]:
        # j=>R
        if flag1==0:
            start_j = bisect_left(C["R"], i)
            if start_j != len(C["R"]):
                for r_index in range(start_j, len(C["R"])):
                    j = C["R"][r_index]
                    start_k = bisect_left(C["B"], j)
                    if start_k != len(C["B"]):
                        for b_index in range(start_k, len(C["B"])):
                            k = C["B"][b_index]
                            if j - i != k - j:
                                res += 1
                    else:
                        break
            else:
                flag1=1
                if flag2==1:
                    break
        # j=>B
        if flag2==0:
            start_j = bisect_left(C["B"], i)
            if start_j != len(C["B"]):
                for b_index in range(start_j, len(C["B"])):
                    j = C["B"][b_index]
                    start_k = bisect_left(C["R"], j)
                    if start_k != len(C["R"]):
                        for r_index in range(start_k, len(C["R"])):
                            k = C["R"][r_index]
                            if j - i != k - j:
                                res += 1
                    else:
                        break
            else:
                flag2=1
                if flag1==1:
                    break

    flag1=0
    flag2=0
    for i in C["B"]:
        # j=>G
        if flag1==0:
            start_j = bisect_left(C["G"], i)
            if start_j != len(C["G"]):
                for g_index in range(start_j, len(C["G"])):
                    j = C["G"][g_index]
                    start_k = bisect_left(C["R"], j)
                    if start_k != len(C["R"]):
                        for r_index in range(start_k, len(C["R"])):
                            k = C["R"][r_index]
                            if j - i != k - j:
                                res += 1
                    else:
                        break
            else:
                flag1=1
                if flag2==1:
                    break

        # j=>R
        if flag2==0:
            start_j = bisect_left(C["R"], i)
            if start_j != len(C["R"]):
                for r_index in range(start_j, len(C["R"])):
                    j = C["R"][r_index]
                    start_k = bisect_left(C["G"], j)
                    if start_k != len(C["G"]):
                        for g_index in range(start_k, len(C["G"])):
                            k = C["G"][g_index]
                            if j - i != k - j:
                                res += 1
                    else:
                        break
            else:
                flag2=1
                if flag1==1:
                    break


    print(res)

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
from heapq import heappush

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

    C={"R":[],"G":[],"B":[]}
    for i in range(N):
        heappush(C[S[i]],i)

    res=0

    flag1=0
    flag2=0
    for i in C["R"]:
        #j=>G
        start_j=bisect_left(C["G"],i)
        if start_j!=len(C["G"]):
            for g_index in range(start_j,len(C["G"])):
                j=C["G"][g_index]
                start_k=bisect_left(C["B"],j)
                if start_k!=len(C["B"]):
                    for b_index in range(start_k,len(C["B"])):
                        k = C["B"][b_index]
                        if j-i!=k-j:
                            res+=1
                else:
                    break
        else:
            flag1=1
            if flag2==1:
                break

        #j=>B
        start_j = bisect_left(C["B"], i)
        if start_j != len(C["B"]):
            for b_index in range(start_j, len(C["B"])):
                j = C["B"][b_index]
                start_k = bisect_left(C["G"], j)
                if start_k != len(C["G"]):
                    for g_index in range(start_k, len(C["G"])):
                        k = C["G"][g_index]
                        if j - i != k - j:
                            res += 1
                else:
                    break
        else:
            flag2=1
            if flag1==1:
                break

    flag1=0
    flag2=0

    for i in C["G"]:
        # j=>R
        start_j = bisect_left(C["R"], i)
        if start_j != len(C["R"]):
            for r_index in range(start_j, len(C["R"])):
                j = C["R"][r_index]
                start_k = bisect_left(C["B"], j)
                if start_k != len(C["B"]):
                    for b_index in range(start_k, len(C["B"])):
                        k = C["B"][b_index]
                        if j - i != k - j:
                            res += 1
                else:
                    break
        else:
            flag1=1
            if flag2==1:
                break
        # j=>B
        start_j = bisect_left(C["B"], i)
        if start_j != len(C["B"]):
            for b_index in range(start_j, len(C["B"])):
                j = C["B"][b_index]
                start_k = bisect_left(C["R"], j)
                if start_k != len(C["R"]):
                    for r_index in range(start_k, len(C["R"])):
                        k = C["R"][r_index]
                        if j - i != k - j:
                            res += 1
                else:
                    break
        else:
            flag2=1
            if flag1==1:
                break

    flag1=0
    flag2=1
    for i in C["B"]:
        # j=>G
        start_j = bisect_left(C["G"], i)
        if start_j != len(C["G"]):
            for g_index in range(start_j, len(C["G"])):
                j = C["G"][g_index]
                start_k = bisect_left(C["R"], j)
                if start_k != len(C["R"]):
                    for r_index in range(start_k, len(C["R"])):
                        k = C["R"][r_index]
                        if j - i != k - j:
                            res += 1
                else:
                    break
        else:
            flag1=1
            if flag2==1:
                break

        # j=>R
        start_j = bisect_left(C["R"], i)
        if start_j != len(C["R"]):
            for r_index in range(start_j, len(C["R"])):
                j = C["R"][r_index]
                start_k = bisect_left(C["G"], j)
                if start_k != len(C["G"]):
                    for g_index in range(start_k, len(C["G"])):
                        k = C["G"][g_index]
                        if j - i != k - j:
                            res += 1
                else:
                    break
        else:
            flag2=1
            if flag1==1:
                break


    print(res)

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
cnt =0
r = []
g = []
b = []
for i in range(n):
  if s[i] == 'R':
    r.append(i)
  if s[i] == 'G':
    g.append(i)
  if s[i] == 'B':
    b.append(i)
for i in range(len(r)):
  for j in range(len(g)):
    for k in range(len(b)):
      a = sorted([r[i],g[j],b[k]])
      if a[2]-a[1] != a[1]-a[0]:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
cnt =0
for i in range(n):
  for j in range(i):
    for k in range(j):
      if s[i]!=s[j] and s[k]!=s[j] and s[i]!=s[k] and i-j!=j-k:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

from collections import defaultdict
d = defaultdict(list)

for ix, c in enumerate(S):
    d[c].append(ix+1)

ans = len(d["B"]) * len(d["G"]) * len(d["R"])

for i in range(1, N+1):
    ci = S[i-1]
    for j in range(i+1, N+1):
        cj = S[j-1]

        if ci == cj:
            continue

        k = 2 * j - i
        if k > j and k <= N:
            ck = S[k-1]
            if ci != ck and cj != ck:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

from collections import defaultdict
d = defaultdict(list)

for ix, c in enumerate(S):
    d[c].append(ix+1)

ans = len(d["B"]) * len(d["G"]) * len(d["R"])

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

Submission status: Time Limit Exceeded

Solution code:
import collections
n = int(input())
s = [''] + list(input())
cnt_1 = collections.Counter(s)

sub = 0
for j in range(1, (n+1)//2):
    for i in range(1, n-2*j+1):
        tmp = s[i] + s[i+j] + s[i+j*2]
        cnt_2 = collections.Counter(tmp)
        if max(cnt_2.values()) == 1:
            sub += 1

print((cnt_1['R']*cnt_1['G']*cnt_1['B'])-sub)

Submission status: Time Limit Exceeded

Solution code:
import itertools


input()
s = list(input())
r =[]
g =[]
b =[]
n= len(s)
ans =0
for i in range(n):
    if s[i]== "R":
        r.append(i+1)
    elif s[i]== "G":
        g.append(i+1)
    else:
        b.append(i+1)

for ri , gi in itertools.product(r,g):
    rg=gi-ri
    if rg+gi in b:
        ans -=1
    if  ri-rg in b:
        ans -=1
    if (gi+ri)/2 in b:
        ans -=1

ans += len(r)*len(b)*len(g)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools


input()
s = list(input())
r =[]
g =[]
b =[]
n= len(s)
ans =0
for i in range(n):
    if s[i]== "R":
        r.append(i+1)
    elif s[i]== "G":
        g.append(i+1)
    else:
        b.append(i+1)

for ss in itertools.product(r,g,b):
    sss =sorted(ss)
    if sss[2] + sss[0] != 2*sss[1]:
        ans +=1

print(ans)

Submission status: Time Limit Exceeded

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

if 'R' != 'G' != 'R' != 'R':
    print('yes')


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

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=0
RGB=[0,0,0]
for i in range(N):
  if S[i]=="R":
    RGB[0]+=1
  if S[i]=="G":
    RGB[1]+=1
  if S[i]=="B":
    RGB[2]+=1
ans=RGB[0]*RGB[1]*RGB[2]    
for i in range(N-2):
  s1=S[i]
  for k in range(i+1,N-1):
    s2=S[k]
    t=k+k-i
    if s1==s2:
      continue
    if t<=N-1:
      s3=S[t]
      if s3!=s1 and s3!=s2:
        ans-=1      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=0
for i in range(N-2):
  s1=S[i]
  for k in range(i+1,N-1):
    s2=S[k]
    for t in range(k+1,N):
      s3=S[t]
      if s1==s2:
        break
      if s3==s1 or s3==s2:
        continue
      elif t-k != k-i:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=0
for i in range(N-2):
  s1=S[i]
  for k in range(i+1,N-1):
    s2=S[k]    
    for t in range(k+1,N):
      s3=S[t]
      if s1==s2:
        break
      if s3==s1 or s3==s2:
        continue
      elif t-k != k-i:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

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

cnt = 0
cnt += S.count("R") * S.count("G") * S.count("B")

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

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

cnt = 0
cnt_str = {
    "R" :0,
    "G" : 0,
    "B" : 0
}
for i in range(N):
    cnt_str[S[i]] += 1

cnt += cnt_str["R"] * cnt_str["G"] * cnt_str["B"]
# print(cnt)
for i in range(1, N + 1):
    for j in range(i + 1, N + 1):
        k = 2 * j - i
        if k > N:
            continue
        if S[i - 1] == S[j - 1] or S[j-1] == S[k-1] or S[i -1] == S[k-1]:
            pass
        else: # 数えられているもので条件2に反するものを引く
            cnt -= 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

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

cnt = 0
for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if i + k == 2 * j:
                pass
            elif S[i] == S[j] or S[j] == S[k] or S[i] == S[k]:
                pass
            else:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
from io import StringIO
import unittest


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

    # 1文字ずつチェックするのでSET。
    s = list(input())

    ans = 0
    for i in range(len(s) - 2):

        for j in range(i+1, len(s) - 1):
            if s[i] is s[j]:
                continue
            target = "RGB".replace(s[i], "").replace(s[j], "")

            ans += s[j:len(s)].count(target)

            if j+(j-i) < len(s) :
                if s[j+(j-i)] is target:
                    ans -= 1

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output =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RRBGGBRBGBRBGBRBBBGBBBB"""
        output = """1800"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
	resolve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = 0
G = 0
B = 0
for i in range(N):
    s = S[i]
    if s == "R":
        R += 1
    elif s == "G":
        G += 1
    elif s == "B":
        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-1 and (S[i] != S[j] and S[j] != S[k] and S[k] != S[i]):
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
R, G, B = [0]*N, [0]*N, [0]*N
rg_set = {("R", "G"), ("G", "R")}
rb_set = {("R", "B"), ("B", "R")}
for i in range(N):
    R[i] += R[i-1] + (1 if S[i] == "R" else 0)
    G[i] += G[i-1] + (1 if S[i] == "G" else 0)
    B[i] += B[i-1] + (1 if S[i] == "B" else 0)
ans = 0
for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            if (i+k)%2 == 0:
                j = (i+k)//2
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i] - (1 if S[j] == "B" else 0)
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i] - (1 if S[j] == "G" else 0)
                else:
                    ans += R[k] - R[i] - (1 if S[j] == "R" else 0)
            else:
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i]
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i]
                else:
                    ans += R[k] - R[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R, G, B = [0]*N, [0]*N, [0]*N
rg_set = {("R", "G"), ("G", "R")}
rb_set = {("R", "B"), ("B", "R")}
for i in range(N):
    R[i] += R[i-1] + (1 if S[i] == "R" else 0)
    G[i] += G[i-1] + (1 if S[i] == "G" else 0)
    B[i] += B[i-1] + (1 if S[i] == "B" else 0)
ans = 0
for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            if (i+k)%2 == 0:
                j = (i+k)//2
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i] - (1 if S[j] == "B" else 0)
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i] - (1 if S[j] == "G" else 0)
                else:
                    ans += R[k] - R[i] - (1 if S[j] == "R" else 0)
            else:
                if (S[i], S[k]) in rg_set:
                    ans += B[k] - B[i]
                elif (S[i], S[k]) in rb_set:
                    ans += G[k] - G[i]
                else:
                    ans += R[k] - R[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
rgb = {"R": [0]*N, "B": [0]*N, "G": [0]*N}
rg_set = {("R", "G"), ("G", "R")}
rb_set = {("R", "B"), ("B", "R")}
for i in range(N):
    rgb["R"][i] += rgb["R"][i-1] + (1 if S[i] == "R" else 0)
    rgb["G"][i] += rgb["G"][i-1] + (1 if S[i] == "G" else 0)
    rgb["B"][i] += rgb["B"][i-1] + (1 if S[i] == "B" else 0)
ans = 0
for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            if (i+k)%2 == 0:
                j = (i+k)//2
                if (S[i], S[k]) in rg_set:
                    ans += rgb["B"][k] - rgb["B"][i] - (1 if S[j] == "B" else 0)
                elif (S[i], S[k]) in rb_set:
                    ans += rgb["G"][k] - rgb["G"][i] - (1 if S[j] == "G" else 0)
                else:
                    ans += rgb["R"][k] - rgb["R"][i] - (1 if S[j] == "R" else 0)
            else:
                if (S[i], S[k]) in rg_set:
                    ans += rgb["B"][k] - rgb["B"][i]
                elif (S[i], S[k]) in rb_set:
                    ans += rgb["G"][k] - rgb["G"][i]
                else:
                    ans += rgb["R"][k] - rgb["R"][i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
k = str(input())
R = [i for i in range(0, n) if k[i] == 'R']
G = [i for i in range(0, n) if k[i] == 'G']
B = [i for i in range(0, n) if k[i] == 'B']

ans = 0
for rx in R:
	for gx in G:
		for bx in B:
			a = [rx, gx, bx]
			a.sort()
			ans += (1 if a[1] - a[0] != a[2] - a[1] else 0)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
s = str(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)

score = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        dist=abs(j-i)
        if (i>j):
            bi = i+dist
            if bi in b:
                score -=1
            bi = j-dist
            if bi in b:
                score -=1
        else:
            bi = j+dist
            if bi in b:
                score-=1
            bi = i-dist
            if bi in b:
                score-=1
        if((i+j)%2==0 and (i+j)/2 in b):
            score-=1
# print(count_)
print(score)

Submission status: Time Limit Exceeded

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

r = []
g = []
b = []
for i in range(n):
    if s[i]=="R":
        r.append(i)
    elif s[i]=="G":
        g.append(i)
    elif s[i]=="B":
        b.append(i)

score = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        dist=abs(j-i)
        if (i>j):
            bi = i+dist
            if bi in b:
                score -=1
            bi = j-dist
            if bi in b:
                score -=1
        else:
            bi = j+dist
            if bi in b:
                score-=1
            bi = i-dist
            if bi in b:
                score-=1
        if((i+j)%2==0 and (i+j)/2 in b):
            score-=1
# print(count_)
print(score)