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()
tmp = Counter(S)
# print(tmp)
r = tmp["R"]
g = tmp["G"]
b = tmp["B"]
ans = 0
# for s in S:
#     if s == "R":
#         r += 1
#     elif s == "G":
#         g += 1
#     else:
#         b += 1

for i in range(len(S) - 2):
    j = 1
    if S[i] == "R":
        ans += g * b
        r -= 1
    elif S[i] == "G":
        ans += r * b
        g -= 1
    else:
        ans += r * g
        b -= 1
    
    while not (i + j + j >= len(S)):
        if S[i] == "R":
            if S[i + j] == "R" or S[i + j+j] == "R":
                j += 1
                continue
            if S[i + j] == "G" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "G":
                ans -= 1
        if S[i] == "G":
            if S[i + j] == "G" or S[i + j+j] == "G":
                j += 1
                continue
            if S[i + j] == "R" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "R":
                ans -= 1
        if S[i] == "B":
            if S[i + j] == "B" or S[i + j+j] == "B":
                j += 1
                continue
            if S[i + j] == "G" and S[i + j + j] == "R" or S[i + j] == "R" and S[i + j + j] == "G":
                ans -= 1
        j += 1
print(ans)

Submission status: Time Limit Exceeded

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

out = 0

def rgb (tmpstr, j, i, s):
    if s[j] != s[i]:
        tmpstr = s[j + 1 :  n]
        remain = "RGB".replace(s[j],"").replace(s[i],"")
        num = tmpstr.count(remain) 
        if (j + (j - i)) < len(s):
            if  s[j + (j - i)]  == remain:
                num = num - 1
        return num
    else:
        return 0
    
    
out = sum([sum([rgb(s[j + 1 :  n], j, i, s) for j in range(i+1, n-1)]) for i in range(n-2)])
#for i in range(n-2):
#    out += sum([rgb(s[j + 1 :  n], j, i, s) for j in range(i+1, n-1)])
print(out)

Submission status: Time Limit Exceeded

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

answer = 0

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

print(answer)

Submission status: Time Limit Exceeded

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

for i in range(0, N-3):
  for j in range(i+1, N-1):
    if S[i] != S[j]:
      if ((S[i] == "R") & (S[j] == "G"))|((S[i] == "G") & (S[j] == "R")): 
        ans = ans + S.count("B", j)
        try:
          if S[j+(j-i)] == "B":
            ans = ans -1
        except:
          None
    if ((S[i] == "R") & (S[j] == "B"))|((S[i] == "B") & (S[j] == "R")): 
        ans = ans + S.count("G", j)
        try:
          if S[j+(j-i)] == "G":
            ans = ans -1
        except:
          None
    if ((S[i] == "G") & (S[j] == "B"))|((S[i] == "B") & (S[j] == "G")): 
        ans = ans + S.count("R", j)
        try:
          if S[j+(j-i)] == "R":
            ans = ans -1
        except:
          None
 

print(ans)

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

print(ans)

Submission status: Time Limit Exceeded

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

import queue

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

     r_list = []
     g_list = []
     b_list = []

     ans = 0

     for i in range(n):
          if s[i] == 'R':
               r_list.append(i)
          elif s[i] == 'G':
               g_list.append(i)
          else:
               b_list.append(i)

     for i in range(n):
          if s[i] == 'R':
               ans += check(i, g_list, b_list)

          if s[i] == 'G':
               ans += check(i, r_list, b_list)

          if s[i] == 'B':
               ans += check(i, r_list, g_list)

     print(ans)

def check(spot, first, second):
     ret = 0
     small_first = []
     big_first = []
     small_second = []
     big_second = []

     for f in first:
          if f < spot: small_first.append(f)
          else: big_first.append(f)

     for s in second:
          if s < spot: small_second.append(s)
          else: big_second.append(s)

     ret += len(small_first) * len(big_second) + len(small_second) * len(big_first)
     for n in small_first:
          ignore = spot + (spot - n)
          if ignore in big_second:
               ret += -1
     
     for n in small_second:
          ignore = spot + (spot - n)
          if ignore in big_first:
               ret += -1
     
     return ret

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=str(input())
list1=[]
for a in s:
    list1.append(a)
u=0
for i in range(1,n+1):
    for j in range(i,n+1):
        for h in range(j,n+1):
            i1=list1[i-1]
            j1=list1[j-1]
            h1=list1[h-1]
            if i1!=j1 and i1!=h1 and j1!=h1 and (j-i)!=(h-j):
                u+=1
print(u)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
T = {'R', 'G', 'B'}
sum_R, sum_G, sum_B = [0], [0], [0]

for i in S:
    sum_R.append(i=='R')
    sum_G.append(i=='G')
    sum_B.append(i=='B')
    
for i in range(1, N+1):
    sum_R[i] = sum_R[i-1] + sum_R[i]
    sum_G[i] = sum_G[i-1] + sum_G[i]
    sum_B[i] = sum_B[i-1] + sum_B[i]
    
dict_RGB = {'R':sum_R, 'G':sum_G, 'B':sum_B}

for i in range(N-2):
    s_i = S[i]
    for j in range(i+1, N-1):
        s_j = S[j]
        if s_i == s_j:
            continue
        
        s_k = list(T - set((s_i, s_j)))[0]
        
        sum_s_k = dict_RGB[s_k]
        ans = ans + sum_s_k[-1] - sum_s_k[j]
        if (2 * j - i < N) and (S[2*j - i]==s_k):
            ans -= 1
        
    
print(ans)

Submission status: Time Limit Exceeded

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

rList = []
gList = []
bList = []

for indx in range(N):
    if S[indx] == 'R':
        rList.append(indx)
    elif S[indx] == 'G':
        gList.append(indx)
    elif S[indx] == 'B':
        bList.append(indx)

NoSum = 0
fList = rList
sList = gList
remain = 'B'

for fItem in fList:
    for sItem in sList:
        if ((2 * max(fItem, sItem) - min(fItem, sItem)) <= N-1):
#            if (S[(2 * max(fItem, sItem) - min(fItem, sItem))] == remain):
            if ((2 * max(fItem, sItem) - min(fItem, sItem)) in bList):
                NoSum += 1
        
        if ((max(fItem, sItem) - min(fItem, sItem)) % 2 == 0):
#            if (S[int((max(fItem, sItem) + min(fItem, sItem))/2)] == remain):
            if (int((max(fItem, sItem) + min(fItem, sItem))/2) in bList):
                NoSum += 1
        
        if ((2 * min(fItem, sItem) - max(fItem, sItem)) >= 0):
#            if  (S[(2 * min(fItem, sItem) - max(fItem, sItem))] == remain):
            if  ((2 * min(fItem, sItem) - max(fItem, sItem)) in bList):
                NoSum += 1

print(len(rList) * len(gList) * len(bList) - NoSum)

Submission status: Time Limit Exceeded

Solution code:
def remaining(s1, s2):
    if not("R" in [s1, s2]):
        return "R"
    elif not("B" in [s1, s2]): 
        return "B"
    else:
        return "G"

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

num_R = [0 for _ in range(N)]
num_G = [0 for _ in range(N)]
num_B = [0 for _ in range(N)]
if S[-1] == "R":
    num_R[-1] = 1
elif S[-1] == "B":
    num_B[-1] = 1
else:
    num_G[-1] = 1

for i in range(N-2, -1, -1): 
    if S[i]=="R":
        num_R[i] = num_R[i+1]+1
        num_G[i] = num_G[i+1]
        num_B[i] = num_B[i+1]
    elif S[i]=="G":
        num_R[i] = num_R[i+1]
        num_G[i] = num_G[i+1]+1
        num_B[i] = num_B[i+1]
    else:
        num_R[i] = num_R[i+1]
        num_G[i] = num_G[i+1]
        num_B[i] = num_B[i+1]+1

if __name__ == "__main__":
    ans = 0
    for i in range(0, N-2):
        for j in range(i+1, N-1):
            if S[i] == S[j]:
                continue
            target = remaining(S[i], S[j])
            if target == 'R':
                num = num_R
            elif target == 'G':
                num = num_G
            else:
                num = num_B

            ans += num[j+1]
            if j+(j-i) < N:
                if S[j+(j-i)]==target:
                    ans -= 1
    print(ans)

Submission status: Time Limit Exceeded

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

from itertools import combinations

lis = list(range(1, N+1))
s = [1 for v in combinations(lis, 3)
     if v[1] - v[0] != v[2] - v[1] and S[v[0]-1] != S[v[1]-1] and S[v[0]-1] != S[v[2]-1] and S[v[1]-1] != S[v[2]-1]]

print(len(s))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N=int(input())
K=list(input())

ans = 0
while len(K)>3:
    a = K.pop()
    for i, b in enumerate(K):
        if b != a:
            k = K[i+1:]
    
            for ii,c in enumerate(k):
                    if (c != b) and (c != a):
                        if (ii != (len(k)-1)/2):
                            ans +=1
                 
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

ans = 0

from itertools import combinations

l = list(range(1, N+1))
c = combinations(l, 3)

def cond2(a,b,c):
    a -= 1
    b -= 1
    c -= 1
    if b-a == c-b:
        return False
    return True

ll = []
for v in c:
    if cond2(v[0], v[1], v[2]):
        ll.append(v)

ans = 0
for t in ll:
    x, y, z = t[0], t[1], t[2]
    x -= 1
    y -= 1
    z -= 1
    if S[x] == S[y] or S[x] == S[z] or S[y] == S[z]:
        continue
    ans += 1

print(ans)

Submission status: Time Limit Exceeded

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):
        x = j + j - i
        if S[i] != S[j] and x <= N - 1:
            if S[i] != S[x] and S[x] != S[j]:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#D
n = int(input())
s = input()
s_l = tuple(s)


cnt = 0
for i in range(n-2):
    for j in range(i,n-1):
        if s_l[i] != s_l[j]:
            for k in range(j,n):
                if s_l[i] != s_l[k] and s_l[j] != s_l[k] and j-i != k-j:
                    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(str(input()))
z = 0
lnr = [i for i, x in enumerate(s) if x == 'R']
lng = [i for i, x in enumerate(s) if x == 'G']
lnb = [i for i in range(n)]
lnb = list(set(lnb) - set(lng) - set(lnr))
for i in lng:
    for j in lnb:
        a = max(i, j)
        b = min(i, j)
        if a + a - b in lnr:
            z += 1
        if (a - b) % 2 == 0:
            if b + ((a - b)//2) in lnr:
                z += 1
        if b * 2 - a >=0:
            if b - (a - b) in lnr:
                z += 1
print(len(lnr) * len(lng) * len(lnb) - z)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
S = read_str()

r = []
g = []
b = []

rap = r.append
gap = g.append
bap = b.append
for i, s in enumerate(S):
    if s == 'R': rap(i)
    elif s == 'G': gap(i)
    else: bap(i)
rgb = [r, g, b]

# print(r, g, b)

def bisect_find_lt(a, x):
    'Find rightmost value less than x'
    i = bisect.bisect_left(a, x)
    if i:
        return i-1
    raise ValueError

def bisect_find_gt(a, x):
    'Find leftmost value greater than x'
    i = bisect.bisect_right(a, x)
    if i != len(a):
        return i
    raise ValueError

def solve():
    ans = 0
    lb = len(b)
    if len(r) == 0 or len(g) == 0 or len(b) == 0:
        return ans
    for i, ng in enumerate(g):
        nr = 0
        nb = 0
        try:
            ri = bisect_find_lt(r, ng)
            bi = bisect_find_gt(b, ng)
            # print(ri, bi)
            for rj in r[0 : ri + 1]:
                for bj in b[bi : lb]:
                    # print(rj, ng, bj)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(g):
        nr = 0
        nb = 0
        try:
            ri = bisect_find_lt(b, ng)
            bi = bisect_find_gt(r, ng)
            # print(ri, bi)
            for rj in b[0 : ri + 1]:
                for bj in r[bi : len(r)]:
                    # print(rj, ng, bj)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(b):
        nr = 0
        nb = 0
        try:
            ri = bisect_find_lt(g, ng)
            bi = bisect_find_gt(r, ng)
            # print(ri, bi)
            for rj in g[0 : ri + 1]:
                for bj in r[bi: len(r)]:
                    # print(rj, ng, bi)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(b):
        nr = 0
        nb = 0
        try:
            ri = bisect_find_lt(r, ng)
            bi = bisect_find_gt(g, ng)
            # print(ri, bi)
            for rj in r[0 : ri + 1]:
                for bj in g[bi : len(g)]:
                    # print(rj, ng, bi)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(r):
        nr = 0
        nb = 0
        try:
            ri = bisect_find_lt(b, ng)
            bi = bisect_find_gt(g, ng)
            # print(ri, bi)
            for rj in b[0 : ri + 1]:
                for bj in g[bi : len(g)]:
                    # print(rj, ng, bi)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(r):
        nr = 0
        nb = 0
        try:
            ri = bisect_find_lt(g, ng)
            bi = bisect_find_gt(b, ng)
            # print(ri, bi)
            for rj in g[0 : ri + 1]:
                for bj in b[bi : lb]:
                    # print(rj, ng, bi)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass


    return ans

if __name__ == "__main__":
    print(solve())

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 - p):
        a = s[p]
        b = s[p + q]
        if a == b:
            continue
        nokori = s[p + q:]
        rgb_list = ['R', 'G', 'B']
        rgb_list.remove(a)
        for e in rgb_list:
            if e not in nokori:
                continue
        rgb_list.remove(b)
        c = rgb_list[0]
        counter += nokori.count(c)
        if q < len(nokori) and nokori[q] == c:
            counter -= 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
#
from collections import deque


def main():
    n = int(input())
    # これだとTLE
    # s = [s for s in input()]
    # count = 0
    # ps = ""
    # pc = 0
    # for j in range(1, n):
    #     if ps == s[j]:
    #         count += pc
    #         continue
    #     pc = 0
    #     for i in range(j - 1, -1, -1):
    #         if s[i] != s[j]:
    #             offset = j - i
    #             for k in range(j + 1, n):
    #                 if s[k] != s[j] and s[k] != s[i]:
    #                     if k - j != offset:
    #                         pc += 1
    #     count += pc
    # print(count)
    # これもTLEだけどちょっと効率いい
    s = deque()
    r = deque()
    g = deque()
    b = deque()
    for i, c in enumerate(input()):
        s.append(c)
        if c == "R":
            r.append(i)
        elif c == "G":
            g.append(i)
        else:
            b.append(i)
    if len(r) == 0 or len(g) == 0 or len(b) == 0:
        print(0)
        return
    count = 0
    t = []
    x = r
    for w in range(3):
        if w == 0:
            x = r
            t = [g, b]
        elif w == 1:
            x = g
            t = [r, b]
        else:
            x = b
            t = [r, g]
        for j in x:
            if s[j] == "R":
                t = [g, b]
            elif s[j] == "G":
                t = [r, b]
            else:
                t = [r, g]
            for z in range(2):
                for i in t[z]:
                    if i >= j:
                        break
                    offset = j - i
                    for num, k in enumerate(t[1 - z]):
                        if k <= j:
                            continue
                        if offset != k - j:
                            count += 1
                        if offset < k - j:
                            count += len(t[1 - z]) - num - 1
                            break
    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def solve(n, s):
    rlist, glist, blist = [0 for _ in range(n)], [0 for _ in range(n)], [0 for _ in range(n)]
    rcount, gcount, bcount = 0, 0, 0
    for i in range(n-1):
        if s[n-1-i] == 'R':
            rcount += 1
        if s[n-1-i] == 'G':
            gcount += 1
        if s[n-1-i] == 'B':
            bcount += 1
        rlist[n-2-i], glist[n-2-i], blist[n-2-i] = rcount, gcount, bcount
        
    ans = 0

    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i] != s[j]:
                if s[i] == 'R':
                    if s[j] == 'G':
                        ans += blist[j] - ( 1 if 2*j-i < n and s[2*j-i] == 'B' else 0)
                    else:
                        ans += glist[j] - (1 if 2*j-i < n and s[2*j-i] == 'G' else 0)
                elif s[i] == 'G':
                    if s[j] == 'R':
                        ans += blist[j] - (1 if 2*j-i < n and s[2*j-i] == 'B' else 0)
                    else:
                        ans += rlist[j] - (1 if 2*j-i < n and s[2*j-i] == 'R' else 0)
                else:
                    if s[j] == 'G':
                        ans += rlist[j] - (1 if 2*j-i < n and s[2*j-i] == 'R' else 0)
                    else:
                        ans += glist[j] - (1 if 2*j-i < n and s[2*j-i] == 'G' else 0)
    
    return ans

def main():
    n = int(input())
    s = input()
    print(solve(n, s))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy


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

    for i in range(1, N-1):
        for j in range(i+1, N):
            if S[i-1] == S[j-1]:
                continue
            RGB_list = []
            RGB_list.append(S[i - 1])
            RGB_list.append(S[j - 1])
            for k in range(j+1, N+1):
                if k == 2 * j - i:
                    continue
                if not (S[k-1] in RGB_list):
                    count += 1

    print(count)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    N=int(input())
    S=input()
    R=[]
    G=[]
    B=[]
    for s in range(N):
        if S[s]=='R':
            R.append(s)
        elif S[s]=='G':
            G.append(s)
        else:
            B.append(s)
    cnt=0
    for r in R:
        for g in G:
            for b in B:
                i=min(min(r,g),b)
                k=max(max(r,g),b)
                j=r+g+b-i-k
                if (j-i)!=(k-j):
                    cnt+=1
    print(cnt)
main()

Submission status: Time Limit Exceeded

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

count = 0

for i in range(n-1):
  for p in range(1,n):
    if i + p > n-1:
      break
      
    for q in range(p+1,n):
      if i+ p+ q > n-1:
        break
      elif p != q:
        j = i + p
        k = j + q
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
          count += 1
        j = i + q
        k = j + p
        if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
          count += 1


print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
  n=int(input())
  s=tuple(input())
  r=[i for i, x in enumerate(s) if x == 'R']
  g=[i for i, x in enumerate(s) if x == 'G']
  rg,ans,R,G=r+g,0,len(r),len(g)
  b=[i for i in range(n) if not i in rg]
  x=n-R-G
  for i in r:
    for j in g:
      ans+=x +(-1 if (i+j)/2 in b else 0) 
      ans+=-1 if ((i+j)+abs(i-j)*3)/2 in b else 0
      ans+=-1 if ((i+j)-abs(i-j)*3)/2 in b else 0
  print(ans)
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for a in range(N-2):
  sa = S[a]
  for b in range(a+1,N-1):  
    sb = S[b]
    if sa != sb:
      for c in range(b+1,N):
        if a-b != b-c:
          sc = S[c]
          if sb != sc and sc != sa:
            count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import defaultdict

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

    base = len(R)*len(G)*len(B)

    cnt = 0
    for r in R:
        for g in G:
            for b in B:
                if abs(b-g) == abs(g-r) or abs(b-r) == abs(r-g) or abs(g-b) == abs(b-r) or abs(g-r) == abs(r-b) or abs(r-b) == abs(b-g) or abs(r-g) == abs(g-b):
                    cnt += 1



    print(base-cnt)  

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from functools import reduce

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

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

N = II()
S = input()

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
if __name__ == "__main__":
	N = int(input())
	S = tuple(input())
	sum = 0
	for i,j,k in combinations(range(N),3):
		if (S[i] != S[j]) and (S[i] != S[k]) and (S[j] != S[k]):
			if (j - i) != (k - j):
				sum += 1
	print(sum)

Submission status: Time Limit Exceeded

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

index = {'R':set([]), 'G':set([]), 'B':set([])}
for i, s in enumerate(S):
    index[s].add(i)
R=index['R']
G=index['G']
B=index['B']
count=len(R)*len(G)*len(B)
for i in R:
    for j in G:
        for k in B:
            maximum = max(i,j,k)
            minimum = min(i,j,k)
            if maximum+minimum==2*(i+j+k-maximum-minimum):
                count-=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

inp = [input() for i in range(2)]

n=int(inp[0])
s=inp[1]

def lop(frm,to):
    l=0
    for i in range(frm, to):
        for j in range(i, to):
            for k in range(j, to):
              if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k] and j-i!=k-j:
                  l+=1
    return l

print(lop(0,n))

Submission status: Time Limit Exceeded

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

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

# R: 0, G: 1, B: 2

# 後ろにいくつ自分と同じ文字があるかの表を作る
DP = []
for i in range(3):
    DP.append([0] * N)

# 末尾だけループの前に代入
if S[-1] == 'R':
    DP[0][-1] = 1
elif S[-1] == 'G':
    DP[1][-1] = 1
elif S[-1] == 'B':
    DP[2][-1] = 1

for i in reversed(range(0, N - 1)):
    if S[i] == 'R':
        DP[0][i] = DP[0][i + 1] + 1
        DP[1][i] = DP[1][i + 1]
        DP[2][i] = DP[2][i + 1]
    elif S[i] == 'G':
        DP[0][i] = DP[0][i + 1]
        DP[1][i] = DP[1][i + 1] + 1
        DP[2][i] = DP[2][i + 1]
    elif S[i] == 'B':
        DP[0][i] = DP[0][i + 1]
        DP[1][i] = DP[1][i + 1]
        DP[2][i] = DP[2][i + 1] + 1

#print(DP)

ans = 0
for i in range(0, N - 2):
    s1 = S[i]
    zero_flag = False
    for j in range(i + 1, N - 1):
        # この先3文字目が存在しないことが分かっていたらjをこれ以上進めても意味がない
        if zero_flag:
            break

        s2 = S[j]
        # i と j が一緒か確認
        if s1 == s2:
            # 一緒であればこの2文字目はスキップ
            continue
        else:
            #print('i: {}, j: {}'.format(i, j))
            # 一緒じゃなければ3文字目(s3)を探す
            ij = (S[i], S[j])
            s3 = 'B'
            int_s3 = 2
            if 'R' not in ij:
                s3 = 'R'
                int_s3 = 0
            elif 'G' not in ij:
                s3 = 'G'
                int_s3 = 1

            # 3文字目の後ろの文字数を取得
            s3_back = DP[int_s3][j + 1]
            #print('s3: {}, s3_back = {}'.format(s3, s3_back))

            # 0ならスキップ
            if s3_back == 0:
                zero_flag = True
                continue

            # j + (j - i)の場所に3文字目があれば文字数を1引く
            if j + (j - i) < N:
                if S[j + (j - i)] == s3:
                    s3_back -= 1
                    #print('kita')

            ans += s3_back

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def problem(s,n):
    t=0
    for  i in range(0,n-2):
        for  j in range(i+1,n-1):
            if s[i]!=s[j]:
                for k in range(j+1,n):
                     if ((j-i)!=(k-j)) and (s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]):
                         t+=1
    return t
n=int(input())
s=input()
print(problem(s,n))

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]:
                if j-i != k-j:
                    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
if n<=2:
    print(0)
else:
    cnt=0
    for i in range(n-2):
        for j in range(i+1,n-1):
            if s[i]==s[j]:
                continue
            else:
                ns=s[j+1:]
                if s[i]=="R":
                    if s[j]=="B":
                        x="G"
                    else:
                        x="B"
                elif s[i]=="G":
                    if s[j]=="B":
                        x="R"
                    else:
                        x="B"
                else:
                    if s[j]=="R":
                        x="G"
                    else:
                        x="R"
                cnt+=ns.count(x)
                if 2*j-i<n:
                    if s[2*j-i]==x:
                        cnt-=1
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    # Positions for each character.
    r = set()
    g = set()
    b = set()
    r_add = r.add
    g_add = g.add
    b_add = b.add
    for ind, c in enumerate(s):
        if c == 'R':
            r_add(ind)
        elif c == 'G':
            g_add(ind)
        else:
            b_add(ind)
    ans = 0
    for i in r:
        for j in g:
            for k in b:
                o, p, q = sorted((i, j, k))
                if p - o != q - p:
                    ans += 1
    return ans


if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
r = []
g = []
b = []
s = len(S)
for i in range(s):
    if S[i] == 'R':
        r.append(i)
    elif S[i] == 'G':
        g.append(i)
    else:
        b.append(i)
lr = len(r)
lg = len(g)
lb = len(b)
cnt = lr * lg * lb
for i in range(lr):
    for j in range(lg):
        r0 = r[i]
        g0 = g[j]
        up = max(r0,g0)+abs(r0-g0)
        dw = min(r0,g0)-abs(r0-g0)
        if up < s and S[up] == 'B':
            cnt -= 1
        if dw >= 0 and S[dw] == 'B':
            cnt -= 1
        if abs(r0-g0)%2 == 0:
            half = min(r0,g0) + abs(r0-g0)//2
            if 0 <= half < s and S[half] == 'B':
                cnt -= 1
print(cnt)

Submission status: Time Limit Exceeded

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

total = 0

for i in range(N):
  for  j in range(i + 1, N):
    if S[i] == S[j]:
      continue
    for k in range(j + 1, N):
      if (S[i] == S[k]) | (S[j] == S[k]):
        continue
      if (j - i) == (k - j):
        continue
      total += 1
      
print(total)

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):
        if S[i]==S[j]:
            continue
        for k in range(j+1,N):
            si = S[i]
            sj = S[j]
            if not ( j-i == k-j or si==S[k] or sj==S[k] ):
                A += 1
print(A)