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:
N = int(input())
S = input()

sum = 0
for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            continue

        RGB = ['R','G','B']
        RGB.remove(S[j])
        RGB.remove(S[i])
        sum += S[(j+1):].count(RGB[0])

        # S[k]
        k = 2 * j - i
        if k < len(S):
            if S[k] == RGB[0]:
                sum -= 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(str(input()))
tot=0
for i in range(n):
    for j in range(i,n):
        if(l[i]!=l[j]):
            for k in range(j,n):
                if((j-i)!=(k-j)):
                    if(l[k]!=l[j] and l[k]!=l[i] ):
                        tot=tot+1
print(tot)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(n-3):
    for j in range(i+1,n-1):
        if s[i] != s[j]:
            for k in range(j+1,n):
                if s[j] != s[k] and s[i] != s[k] and j - i != k - j:
                    ans += 1
                    #print('{} {} {} '.format(i,j,k))
    

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            if not S[i]==S[j]:
                if not S[i]==S[k]:
                    if not S[j]==S[k]:
                        if not j-i == k-j:
                            A += 1
print(A)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
count=0
N=int(input())
slist=deque(list(input()))
for a in range(N-2):
    l=slist[a]
    for b in range(a+1,N-1):
        m=slist[b]
        if m != l:
            for c in range(b+1,N):
                n=slist[c]
                if m!=n and l!=n and b-a != c-b:
                    count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
cnt=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]!=S[k] and S[i]!=S[k] and (j-i!=k-j):
                cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

    su = 0
    for k in range(2, N):
        for j in range(1, k):
            if S[j] == S[k]:
                continue
            for i in range(j):
                # print(i, j, k)
                if j-i == k-j:
                    pass
                elif S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                    su += 1
                # su += 1
    print(su)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
import sys

input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]

n=I()
s=input()
r=[]
g=[]
t=[]
for i in range(n):
    if s[i:i+1]=="R":
        r.append(i)
    elif s[i:i+1]=="G":
        g.append(i)
    else:
        t.append(i)
R=len(r)
G=len(g)
ans=R*G*(n-R-G)
for a in r:
    for b in g:
        if 2*b-a in t:
            ans-=1
        if 2*a-b in t:
            ans-=1
        if (a+b)%2==0 and (a+b)//2 in t:
            ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
cnt_r = [0]*N
cnt_g = [0]*N
cnt_b = [0]*N
for i in range(N-1,-1,-1):
    if S[i] == 'R':
        cnt_r[i] += 1
    if S[i] == 'G':
        cnt_g[i] += 1
    if S[i] == 'B':
        cnt_b[i] += 1
    if i != 0:
        cnt_r[i-1] += cnt_r[i]
        cnt_g[i-1] += cnt_g[i]
        cnt_b[i-1] += cnt_b[i]
# print(cnt_r)
# print(cnt_g)
# print(cnt_b)
ans = 0
for i in range(N):
    if S[i] == 'R':
        for j in range(i+1,N):
            if S[j] == 'G':
                if j+1 >= N:
                    continue
                ans += cnt_b[j+1]

                if j + (j-i) >= N:
                    continue
                if S[j+(j-i)] == 'B':
                    ans -= 1

for i in range(N):
    if S[i] == 'R':
        for j in range(i + 1, N):
            if S[j] == 'B':
                if j + 1 >= N:
                    continue
                ans += cnt_g[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'G':
                    ans -= 1

for i in range(N):
    if S[i] == 'G':
        for j in range(i + 1, N):
            if S[j] == 'R':
                if j + 1 >= N:
                    continue
                ans += cnt_b[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'B':
                    ans -= 1

for i in range(N):
    if S[i] == 'G':
        for j in range(i + 1, N):
            if S[j] == 'B':
                if j + 1 >= N:
                    continue
                ans += cnt_r[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'R':
                    ans -= 1

for i in range(N):
    if S[i] == 'B':
        for j in range(i + 1, N):
            if S[j] == 'G':
                if j + 1 >= N:
                    continue
                ans += cnt_r[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'R':
                    ans -= 1

for i in range(N):
    if S[i] == 'B':
        for j in range(i + 1, N):
            if S[j] == 'R':
                if j + 1 >= N:
                    continue
                ans += cnt_g[j + 1]

                if j + (j - i) >= N:
                    continue
                if S[j + (j - i)] == 'G':
                    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

col_lis = [0 if c == "R" else 1 if c == "G" else 2 for c in S]
col_lis = np.asarray(col_lis)

ans = 0

r_count = 0
g_count = 0
b_count = 0

color_table = []

for color in col_lis:
    if color == 0:
        r_count += 1
    elif color == 1:
        g_count += 1
    else:
        b_count += 1
    color_table.append([r_count, g_count, b_count])

ans = 0

for center in range(1, N - 1):
    for right in range(center + 1, N):
        if col_lis[right] == col_lis[center]:
            continue
        left = 3 - col_lis[right] - col_lis[center]
        ans += color_table[center - 1][left]
        if center * 2 - right >= 0 and col_lis[center * 2 - right] == left:
            ans -= 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
if n<=3:
    print(0)
    exit()
r=[]
g=[]
b=[]
for i,c in enumerate(s):
    if c=='R':
        r.append(i)
    elif c=='G':
        b.append(i)
    else:
        g.append(i)
ans=0
for i in r:
    for j in g:
        for k in b:
            l=sorted([i,j,k])
            if l[1]-l[0]!=l[2]-l[1]:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

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

count = [{"R": 0, "G": 0, "B": 0} for _ in range(L)]

count[L-1][s[L-1]] += 1

for i2 in range(L - 1):
    i = L - i2 - 1
    count[i - 1] = count[i].copy()
    count[i - 1][s[i - 1]] += 1

ans = 0
for i in range(L - 2):
    for j2 in range(L - i - 2):
        j = i + j2 + 1
        if s[i] == s[j]:
            continue
        if s[i] != "R" and s[j] != "R":
            key = "R"
        elif s[i] != "G" and s[j] != "G":
            key = "G"
        else:
            key = "B"
        ans += count[j + 1][key]
        if j * 2 - i < L and s[j * 2 - i] == key:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(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)
count = 0


for a in R:
    for b in G:
        for c in B:
            num = [a, b, c]
            num.sort()
            if num[1] - num[0] != num[2] - num[1]:
                count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

retval=0

for i in range(N):
    for j in range(i+1,N):
        if S[i]==S[j]:
            continue

        rest=list(set(['R','G','B'])-set([S[i],S[j]]))[0]
        for k in range(j+1,N):
            if S[k]==rest and k-j!=j-i:
                retval+=1

print(retval)

Submission status: Time Limit Exceeded

Solution code:
import sys
import copy
import math
import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
from decimal import *

import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

if __name__ == '__main__':
    n = int(input())

    a = input()
    listr = [0]*n
    listg = [0]*n
    listb = [0]*n
    r =a.count("R")
    g =a.count("G")
    b =a.count("B")

    count = 0

    for i in range(n):

        if a[i] == "R":
            r -=1
        if a[i] == "G":
            g -=1
        if a[i] == "B":
            b -=1
        listr[i] =r
        listb[i] =b
        listg[i] =g

    for i in range(n-2):
        for j in range(i+1,n-1):
            if a[i] != a[j]:
                if a[i] =="R" and a[j] =="G":
                    count += listb[j]
                    if (j-i)+j < len(a) and a[(j-i)+j] =="B" and listb[j] >0:
                        count -=1
                    continue
                if a[i] =="R" and a[j] =="B":
                    count += listg[j]
                    if (j-i)+j < len(a) and a[(j-i)+j] =="G" and listg[j] >0:
                        count -=1
                    continue
                if a[i] =="G" and a[j] =="B":
                    count += listr[j]
                    if (j-i)+j < len(a) and a[(j-i)+j] =="R" and listr[j] >0:
                        count -=1
                    continue
                if a[i] =="G" and a[j] =="R":
                    count += listb[j]
                    if (j-i)+j < len(a) and a[(j-i)+j] =="B" and listb[j] >0:
                        count -=1
                    continue

                if a[i] =="B" and a[j] =="G":
                    count += listr[j]
                    if (j-i)+j < len(a) and a[(j-i)+j] =="R" and listr[j] >0:
                        count -=1
                    continue

                if a[i] =="B" and a[j] =="R":
                    count += listg[j]
                    if (j-i)+j < len(a) and a[(j-i)+j] =="G" and listg[j] >0:
                        count -=1
                    continue


    print(count)

Submission status: Time Limit Exceeded

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

A=0

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
# 1<=i<j<k<=n
# j-i!=k-j
# si!=sj, sj!=sk, si!=sk
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        if s[i]==s[j]:
            continue
        ans+=len(s[j+1:].replace(s[i],'').replace(s[j],''))
        if 2*j-i < n and s[2*j-i] != s[i] and s[2*j-i] != s[j]:
            ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
for i in range(N):
    for j in range(i+1, N):
        if(S[i] != S[j]):
            for k in range(j+1, N):
                if (S[k] == S[i]) or ( S[k] == S[j]) or ( j-i == k-j):
                    continue;
                count+= 1
print(count)

Submission status: Time Limit Exceeded

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

rr = [0]*N
gr = [0]*N
br = [0]*N

r = 0
g = 0
b = 0
for i in range(N-1, -1, -1):
    if S[i] == "R":
        r += 1
    if S[i] == "G":
        g += 1
    if S[i] == "B":
        b += 1
    rr[i] = r
    gr[i] = g
    br[i] = b

wa = {"R":rr, "G":gr, "B":br}

res = 0

for i in range(N):
    c1 = S[i]
    for j in range(i+1, N):
        c2 = S[j]
        if c1 == c2:
            continue
        c = (c1, c2)
        if "R" not in c:
            c3 = "R"
        elif "G" not in c:
            c3 = "G"
        else:
            c3 = "B"

        for k in range(j+1, N):
            if wa[c3][k] == 0:
                break
            if S[k] == c3:
                res += wa[c3][k]
                x = i+(j-i)*2
                if x < N:
                    if S[x] == c3:
                        res -= 1
                        pass
                break

print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = list(input())
L = [[], [], []]
for i in range(N):
  if s[i] == 'R':
    L[0].append(i)
  elif s[i] == 'G':
    L[1].append(i)
  else:
    L[2].append(i)
ans = len(L[0]) * len(L[1]) * len(L[2])
for i in L[0]:
  for j in L[1]:
    if (2 * i - j) in L[2]:
      ans = ans - 1
    if (2 * j - i) in L[2]:
      ans = ans - 1
    if (i - j) % 2 == 0 and int((i+j)/2) in L[2]:
      ans = ans - 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
red = s.count('R')
green = s.count('G')
blue = s.count('B')
res = red * green * blue
k = n//2
for i in range(n-2):
    for t in range(i+1,((n-1+i)//2)+1):
        if set((s[i],s[t],s[2*t-i])) == set(('R','G','B')):
            res -= 1
print(res)

Submission status: Time Limit Exceeded

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

    ans = 0

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

    print(ans)


resolve()

Submission status: Time Limit Exceeded

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

result = 0

if n <= 2:
    print(0)
else:
    for i in range(n-2):
        for j in range(i+1, n-1):
            for k in range(j+1, n):
                if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                    if j-i != k-j:
                        result +=1
print(result)

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)
lr, lg, lb = len(r), len(g), len(b)
ans = lr * lg * lb
for i in r:
    for j in g:
        a = 2 * j - i
        c = 2 * i - j
        if 0 <= a and a <= n-1 and a in b:
            ans -= 1
        if 0 <= c and c <= n-1 and c in b:
            ans -= 1
        if (i + j) % 2 == 0 and (i + j) / 2 in b:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
n = int(input())
s = input()
c_dic = defaultdict(list)
for idx,i in enumerate(s):
    c_dic[i].append(idx)
ans = 0
for i in c_dic['R']:
    for j in c_dic['G']:
        ma = max(i,j)
        mi = min(i,j)
        for l in c_dic['B']:
            ma1 = max(ma,l)
            middle = max(min(ma,l),mi)
            mi1 = min(l,mi)
            if ma1 - middle !=  middle - mi1:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

rgb_dic = {
    "R": [],
    "G": [],
    "B": []
}
for i, s in enumerate(S):
    rgb_dic[s].append(i)

R_list = rgb_dic["R"]
G_list = rgb_dic["G"]
B_list = rgb_dic["B"]

cnt = 0
idx_set = set()
for i in R_list:
    for j in G_list:
        for k in B_list:
            if abs(i-j) != abs(j-k) and abs(i-j) != abs(i-k) and abs(j-k) != abs(i-k):
                cnt += 1 
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
c = 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])&(S[j]!=S[k]))&(S[k]!=S[i]) :
                if (j-i)!=(k-j):
                    c = c + 1
print(c)

Submission status: Time Limit Exceeded

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

def check(a,b,c):
    if abs(a-b) == abs (a-c) or abs(b-a) == abs (b-c) or abs(c-a) == abs (c-b):
        return True
    else:
        return False

R=[]
G=[]
B=[]
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)
count = 0
for i in range(len(R)):
    for j in range(len(G)):
        for k in range(len(B)):
            if check(R[i],G[j],B[k]):
                count +=1
print(len(R)*len(G)*len(B)-count)

Submission status: Time Limit Exceeded

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

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


print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


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

    R = []
    G = []
    B = []

    for i, s in enumerate(S):
        if s == 'R':
            R.append(i)
            continue
        elif s == 'G':
            G.append(i)
            continue
        elif s == 'B':
            B.append(i)
    

    total = 0

    for r in R:
        for g in G:
            for b in B:
                li = sorted([r, g, b])
                if li[1] - li[0] != li[2] - li[1]:
                    total += 1
    
    print(total)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def bool3(a,b,c):
    if (a!=b) and (b!=c) and (c!=a):
        return True
    else:
        return False

def bool2(a,b,c):
    if a-b != b-c:
        return True
    else:
        return False

def main():
    n = int(input())
    s = input()
    ans = 0

    for i in range(n):
        for j in range(i):
            for k in range(j):
                if bool3(s[i],s[j],s[k]) and bool2(i,j,k):
                    ans += 1
    print(ans)

if __name__ == '__main__':
    main()

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):
        a = s[i]
        b = s[j]
        if a != b:
            for k in range(j+1,n):
                c = s[k]
                if (j-i != k-j) and (a != c and b != c):
                    ans +=1
print(ans)

Submission status: Time Limit Exceeded

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



for i in range(n):
    for j in range(1, n):
        for k in range(3, n):
            if (j - i != k - j) & (i < j < k):
                if (s[i] != s[j]) & (s[j] != s[k]) & (s[k] != s[i]):
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

R, G, B = [], [], []
for i, c in enumerate(s):
  if c == 'R': R.append(i)
  elif c == 'G': G.append(i)
  else: B.append(i)

ans = 0
for r in R:
  for g in G:
    for b in B:
      p = list(sorted([r, g, b]))
      if p[1] - p[0] != p[2] - p[1]: ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

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

    counter = Counter(s)
    rs = counter['R']
    gs = counter['G']
    bs = counter['B']
    

    rcnt, gcnt, bcnt = 0, 0, 0
    rl, gl, bl = list(), list(), list()

    for i in range(N):
        if s[i] == 'R':
            rcnt += 1
        elif s[i] == 'G':
            gcnt += 1
        elif s[i] == 'B':
            bcnt += 1
        
        rl.append(rs-rcnt)
        gl.append(gs-gcnt)
        bl.append(bs-bcnt)
    ans = 0
    
    for i in range(N-2):
        if s[i] != 'R':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'G':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'B'
                ok = int(ok)
                ans += bl[j] - ok
            else:
                ans += bl[j]

    
    #RBG
    for i in range(N-2):
        if s[i] != 'R':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'B':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'G'
                ok = int(ok)
                ans += gl[j] - ok
            else:
                ans += gl[j]
    
    for i in range(N-2):
        if s[i] != 'B':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'R':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'G'
                ok = int(ok)
                ans += gl[j] - ok
            else:
                ans += gl[j]
    
    for i in range(N-2):
        if s[i] != 'B':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'G':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'R'
                ok = int(ok)
                ans += rl[j] - ok
            else:
                ans += rl[j]
    for i in range(N-2):
        if s[i] != 'G':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'R':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'B'
                ok = int(ok)
                ans += bl[j] - ok
            else:
                ans += bl[j]
    for i in range(N-2):
        if s[i] != 'G':
            continue
        for j in range(i+1, N-1):
            if s[j] != 'B':
                continue
            if j+abs(j-i) <N:
                ok = s[j+abs(j-i)] == 'R'
                ok = int(ok)
                ans += rl[j] - ok
            else:
                ans += rl[j]



    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

r = [i for i, x in enumerate(s) if x == 'R']
g = [i for i, x in enumerate(s) if x == 'G']
b = [i for i, x in enumerate(s) if x == 'B']
ans = 0

for x in r:
    n = [i for i in g if i>x]
    for y in n:
        m = [i for i in b if i>y]
        for z in m:
            if y-x != z-y:
                ans += 1

for x in r:
    n = [i for i in b if i>x]
    for y in n:
        m = [i for i in g if i>y]
        for z in m:
            if y-x != z-y:
                ans += 1

for x in b:
    n = [i for i in r if i>x]
    for y in n:
        m = [i for i in g if i>y]
        for z in m:
            if y-x != z-y:
                ans += 1
                
for x in b:
    n = [i for i in g if i>x]
    for y in n:
        m = [i for i in r if i>y]
        for z in m:
            if y-x != z-y:
                ans += 1
                
for x in g:
    n = [i for i in r if i>x]
    for y in n:
        m = [i for i in b if i>y]
        for z in m:
            if y-x != z-y:
                ans += 1 
                
for x in g:
    n = [i for i in b if i>x]
    for y in n:
        m = [i for i in r if i>y]
        for z in m:
            if y-x != z-y:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

counter = 0
for p in range(n):
    for q in range(n):
        if p >= q:
            continue
        for r in range(n):
            if q >= r:
                continue
            a = s[p]
            b = s[q]
            c = s[r]
            if a != b and b != c and c != a and r - q != q - p:
                # print(a, b, c, p, q, r)
                counter += 1
print(counter)

Submission status: Time Limit Exceeded

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

ans = 0
ans_ = 0
for i in range(len(S)):
  for j in range(len(S)-i-1):
    for k in range(len(S)-i-j-2):
      if j != k and S[i] != S[i+k+1] and S[i] != S[i+k+j+2] and S[i+k+j+2] != S[i+k+1]:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
Rlist = []
Glist = []
Blist = []
for x in range(len(s)):
    if s[x] == "R": Rlist.append(x)
    elif s[x] == "G": Glist.append(x)
    elif s[x] == "B": Blist.append(x)

def Condition(a, b, c):
    result = True
    ma = max(a, b, c)
    mi = min(a, b, c)
    if mi != a and ma != a: result = Condition(mi, a, ma)
    elif mi != c and ma != c: result = Condition(mi, c, ma)
    elif c < a: result = Condition(c, b, a)
    else: result = b - a != c - b
    return result

ans = 0
for r in Rlist:
    for g in Glist:
        for b in Blist:
            if Condition(r, g, b) == True: ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(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:
import sys
import math
import collections
from collections import deque
from functools import reduce
import heapq
n = int(input())
#n, k = map(int, sys.stdin.readline().split())
s = input()
#a = list(map(int, sys.stdin.readline().split()))

def main():
    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)

    R = len(r)
    G = len(g)
    B = len(b)

    if min(R, G, B) == R:
        if max(R, G, B) == G:
            w = r
            x = b
            y = g
        else:
            w = r
            x = g
            y = b
    elif min(R, G, B) == G:
        if max(R, G, B) == R:
            w = g
            x = b
            y = r
        else:
            w = g
            x = r
            y = b
    else:
        if max(R, G, B) == R:
            w = b
            x = g
            y = r
        else:
            w = b
            x = r
            y = g


    ans = 0
    z = len(y)
    for i in range(len(w)):
        for j in range(len(x)):
            d = abs(w[i] - x[j])
            e = 0
            p = min(w[i], x[j]) - d
            q = max(w[i], x[j]) + d
            if (w[i] + x[j]) % 2 == 0:
                t = int((w[i] + x[j]) / 2)
            else:
                t = 10000
            if p in y:
                e += 1
            if q in y:
                e += 1
            if t in y:
                e += 1

            ans += (z - e)

    print(ans)
            
main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

n = int(input())
s = input()
r, g, b = [], [], []

for i, char in enumerate(s):
    if char == 'R': r.append(i)
    elif char == 'G': g.append(i)
    else: b.append(i)

count = 0
# for config in product(r,g,b):
#     a, b, c = config
#     a, b, c = sorted((a, b, c))

#     if (b-a)!=(c-b): count += 1

# print(count)

for i in r:
    for j in g:
        for k in b:
            if i<j<k:
                if (j-i)!=(k-j): count += 1
            if j<i<k:
                if (i-j)!=(k-i): count += 1
            if k<i<j:
                if (i-k)!=(j-i): count += 1
            if i<k<j:
                if (k-i)!=(j-k): count += 1
            if j<k<i:
                if (k-j)!=(i-k): count += 1
            if k<j<i:
                if (j-k)!=(i-j): count += 1
            

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

def sol(a,b,nu,checker):
    if a==b:
        return None,0
    loop=['R','G','B']
    for each in loop:
        if a!=each and b!=each:
            res=(checker[-1][each]-checker[nu][each])
            return each,res

n=int(input())
s=input()
#import random
#n=4000
#hoge=['R','G','B']
#s=[hoge[i%3] for i in range(n)]
#s="".join(s)
rgb_init={'R':0,'G':0,'B':0}
store=[rgb_init.copy()]
for i,v in enumerate(s):
    tmp=store[i].copy()
    tmp[v]+=1
    store.append(tmp)

ans=0
for i in range(n):
    for j in range(i+1,n):
        al,val=sol(s[i],s[j],j+1,store)
        if (al and 2*j-i<n)and s[2*j-i]==al:
            val-=1
        ans+=val
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=input()
RGB=[[] for i in range(3)]
for i in range(n):
    if S[i]=="R":
        RGB[0].append(i)
    elif S[i]=="G":
        RGB[1].append(i)
    else:
        RGB[2].append(i)
L=[len(RGB[0]),len(RGB[1]),len(RGB[2])]
m=L.index(max(L))
A=[]
for i in range(3):
    if i==m:
        continue
    else:
        A.append(i)
C=RGB[A[0]]
D=RGB[A[1]]
E=RGB[m]

ans=0
for i in range(L[A[0]]):
    for j in range(L[A[1]]):
        r=C[i]
        g=D[j]
        d=abs(r-g)
        cnt=0
        if d%2==0:
            if (r+g)//2 in E:
                cnt+=1
        if r<g:
            if g+d in E:
                cnt+=1
            if r-d in E:
                cnt+=1
        if r>g:
            if r+d in E:
                cnt+=1
            if g-d in E:
                cnt+=1
        ans+=L[m]-cnt
print(ans)