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()
count = 0
for i in range(1,n-1):
    for j in range(1,n):
        for k in range(1,n+1):
            if not j-i == k-j:
                if i<j<k:
                    if not s[i-1] == s[j-1]:
                        if not s[i-1] == s[k-1]:
                            if not s[j-1] == s[k-1]:
                                count +=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(str,list(input())))
c = 0

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

print(c)

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, x in enumerate(s) if x == 'B']     
for i in lnr:
    for j in lnb:
        ln = sorted([i, j])
        if ln[0] + ln[0] - ln[1] in lng:
            z += 1
        if (ln[0] - ln[1]) % 2 == 0 and ln[1] + ((ln[0] - ln[1])//2) in lng:
            z += 1
        if ln[1] - (ln[0] - ln[1]) in lng:
            z += 1
print(len(lnr) * len(lng) * len(lnb) - z)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
lst=list(s)
r=[i for i,j in enumerate(lst) if j=="R"]
g=[i for i,j in enumerate(lst) if j=="G"]
b=[i for i,j in enumerate(lst) if j=="B"]
comb=len(r)*len(g)*len(b)
out=0
for i in r:
  for j in g:
    for k in b:
      if (i+j)==2*k or (j+k)==2*i or (k+i)==2*j:
        out+=1
print(comb-out)

Submission status: Time Limit Exceeded

Solution code:
def binary_search(target, left, right, nums):
    if(left > right):
        return -1
    mid = (right-left)//2 + left
    if(nums[mid] == target):
        return mid
    elif(nums[mid] < target):
        return binary_search(target, mid+1, right, nums)
    else:
        return binary_search(target, left, mid-1, nums)
def Main(N, S):
    ans = 0
    d = {'R':[], 'G':[], 'B':[]}
    for i, c in enumerate(S):
        d[c].append(i)
    #print(d)
    lenb = len(d['B'])
    for i in d['R']:
        for j in d['G']:
            for k in d['B']:
                if abs(j-i) != abs(k-j) and abs(j-i) != abs(k-i) and abs(k-j) != abs(i-k):
                    ans += 1
    return ans
        
    

def main():
    N = int(input())
    S = str(input())
    print(Main(N,S))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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


ans = N * (N - 1) * (N - 2) // 6
for i in range(N):
  for j in range(i+1, N):
    if S[j] == S[i]:
      ans -= N - 2
    else:
      if 2 * j - i < N:
        if len(set([S[2*j-i], S[i], S[j]])) == 3:
          ans -= 1
          
ans += r * (r - 1) * (r - 2) // 3
ans += g * (g - 1) * (g - 2) // 3
ans += b * (b - 1) * (b - 2) // 3

print(ans)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())

b=input()
c=a-1
d=0

for i in range(c):
  for j1 in range(c-i):
    j=j1+i+1
    if b[i]!=b[j]:
      for k1 in range(c-j):
        k=k1+j+1
        if b[k]!=b[i] and b[k]!=b[j] and j-i != k-j:
          d=d+1
print(d)

Submission status: Time Limit Exceeded

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

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

count = 0
for r in r_list:
    for g in g_list:
        for b in b_list:
            List = sorted([r, g, b])
            if List[0] + List[2] != List[1] * 2:
                count += 1

print(count)

Submission status: Time Limit Exceeded

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

# keyは(色、jの位置)のタプル
dp = {}

ans = 0

for i in range(n - 2):
    c_i = S[i]
    for j in range(i + 1, n - 1):
        if S[j] == c_i:
            continue
        c_j = S[j]
        d_ij = j - i
        if (c_i, c_j, j) in dp:
            ans += dp[(c_i, c_j, j)]
            if j + d_ij < n:
                c_k = S[j + d_ij]
                if c_i != c_j and c_j != c_k and c_k != c_i:
                    ans -= 1
        else:
            tmp = 0
            for k in range(j + 1, n):
                c_k = S[k]
                if c_i != c_j and c_j != c_k and c_k != c_i:
                    tmp += 1
            dp[(c_i, c_j, j)] = tmp
            if j + d_ij < n:
                c_k = S[j + d_ij]
                if c_i != c_j and c_j != c_k and c_k != c_i:
                    tmp -= 1
            ans += tmp
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(N):
    Si = S[i]
    for j in range(i+1,N):
        Sj = S[j]
        if Si==Sj:
            continue
        for k in range(j+1, N):
            if Si==S[k] or S[k]==Sj:
                continue
            if 2*j != k+i:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
R=[]
G=[]
B=[]
ans = 0
for i in range(n):
    if s[i]=="R":
        R.append(i)
    elif s[i]=="G":
        G.append(i)
    else:
        B.append(i)
for i in R:
    for j in G:
        for k in B:
            if i<j and j<k and j-i!= k-j:
                ans += 1
for i in R:
    for j in B:
        for k in G:
            if i<j and j<k and j-i!= k-j:
                ans += 1
for i in G:
    for j in R:
        for k in B:
            if i<j and j<k and j-i!= k-j:
                ans += 1
for i in G:
    for j in B:
        for k in R:
            if i<j and j<k and j-i!= k-j:
                ans += 1
for i in B:
    for j in R:
        for k in G:
            if i<j and j<k and j-i!= k-j:
                ans += 1
for i in B:
    for j in G:
        for k in R:
            if i<j and j<k and j-i!= k-j:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
cnt=0
from itertools import combinations
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 j-i==k-j :
                continue
            if  s[j]!=s[k] and s[i]!=s[k]:
                    cnt+=1

print(cnt)

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(2, N):
        if j <= i:
            continue
        for k in range(3, N+1):
            if k <= j:
                continue
            if j-i != k-j and S[i-1] != S[j-1] and S[i-1] != S[k-1] and S[k-1] != S[j-1]:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

r = []
g = []
b = []
cnt = 0

for i in S:
  if i == 'R':
    r.append(cnt)
    S = S[1:]
  elif i == 'G':
    g.append(cnt)
    S = S[1:]
  elif i == 'B':
    b.append(cnt)
    S = S[1:]
  cnt += 1  
  
dup = 0 
for x in r:
  for y in g:
    if (x+y)/2 in b:
      dup += 1
      
for x in g:
  for y in b:
    if (x+y)/2 in r:
      dup += 1

for x in b:
  for y in r:
    if (x+y)/2 in g:
      dup += 1
        
print(len(r)*len(g)*len(b)-dup)

Submission status: Time Limit Exceeded

Solution code:
import sys
si = sys.stdin.readline

d = ['R', 'G', 'B']


def main():
    n = int(si())
    l = list(si())
    dc = d[:]
    count = 0
    for i in range(n):
        r1 = l[i]
        dc.remove(r1)
        for j in range(i+1, n):
            if l[j] in dc:
                r2 = l[j]
                dc.remove(r2)
                for k in range(j+1, n):
                    if l[k] in dc:
                        count += 1
                if (j+j-i) < n and l[j+j-i] in dc:
                    count -= 1
                dc += [r2]
        dc += [r1]
    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

i2 = range(len(S) - 3)
j2 = len(S) - 1
k2 = len(S)
count = 0
for i in i2:
    for j in range(i + 1, j2):
        if S[i] == S[j]: continue
        for k in range(j + 1, k2):
            if j - i == k -j:
                continue
            if S[i] != S[k] and S[j] != S[k]:
                count += 1
                

print(count)

Submission status: Time Limit Exceeded

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

ans = 0

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

for i in range(len(S)):
    if S[i] == 'G':
        for j in range(i+1, len(S)):
            if S[j] != 'G':
                for k in range(j+1, len(S)):
                    if S[k] != 'G' and S[k] != S[j]:
                        if j-i != k-j:
                            ans +=1
for i in range(len(S)):
    if S[i] == 'B':
        for j in range(i+1, len(S)):
            if S[j] != 'B':
                for k in range(j+1, len(S)):
                    if S[k] != 'B' and S[k] != S[j]:
                        if j-i != k-j:
                            ans +=1
print(ans)

Submission status: Time Limit Exceeded

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

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)
ans=0
for l in r:
  for m in g:
    for k in b:
      if abs(m-l)!=abs(k-m) and abs(k-l)!=abs(k-m) and abs(k-l)!=abs(l-m):
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s_r=input()
s=[]
for a in range(n):
    if s_r[a]=="R":
        s.append(0)
    elif s_r[a]=="B":
        s.append(1)
    else:
        s.append(2)
count=0
i=0
j=0
k=0
for i in range(n):
    for j in range(n-1-i):
        for k in range(n-2-i-j):
            if (s[i]!=s[j+i+1] and s[i]!=s[k+j+i+2] and s[j+i+1]!=s[k+j+i+2] and (j+i+1)-i!=(k+j+i+2)-(j+i+1)):
                count+=1
            else:
                pass
            k+=1
        j+=1
    i+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = list(str(input()))
    print()
    sum = 0
    for i in range(0, N-2):
        si = S[i]
        for j in range(i, N-1):
            sj = S[j]
            for k in range(j, N):
                sk = S[k]
                #if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                if j - i != k -j:
                    if si != sj and si != sk and sj != sk:
                    #if j - i != k -j:
                        sum += 1
    print(sum)



main()

Submission status: Time Limit Exceeded

Solution code:

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

r = [i+1 for i in range(len(s)) if s[i]=='R']
g = [i+1 for i in range(len(s)) if s[i]=='G']
b = [i+1 for i in range(len(s)) if s[i]=='B']

if len(r)==0 or len(g)==0 or len(b)==0:
   print(0)
   exit()

from itertools import product

cand1 = list(product(r, g))
cand2 = list(product(cand1, b))
ans = 0
for x in cand2:
   tmp = sorted([x[0][0], x[0][1], x[1]])
   i, j, k = tmp[0], tmp[1], tmp[2]
   c = set(s[i-1] + s[j-1] + s[k-1])
   #print(i, j, k, c)
   if len(c) == 3 and j-i!=k-j:
      ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

import bisect

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


ans = N * (N - 1) * (N - 2) // 6
for i in range(N):
  for j in range(i+1, N):
    if S[j] == S[i]:
      ans -= N - 2
    else:
      if 2 * j - i < N:
        if len(set([S[2*j-i], S[i], S[j]])) == 3:
          ans -= 1
          
ans += r * (r - 1) * (r - 2) // 3
ans += g * (g - 1) * (g - 2) // 3
ans += b * (b - 1) * (b - 2) // 3

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def checkCount(ary, Sk, ji):
    cnt = ary.count(Sk)
    if len(ary) > ji - 1:
        if ary[ji-1] == Sk:
            cnt -= 1
    return cnt


def main():
    n = int(input())
    ary = list(input())
    ans = 0
    for i in range(n-3):
        Si = ary.pop(0)
        for j, Sj in enumerate(ary):
            if Sj != Si:
                tmp = ary[j + 1:]
                if tmp != []:
                    ji = (j + 1 + i) - i
                    if Si == 'R':
                        if Sj == 'G':
                            ans += checkCount(tmp, 'B', ji)
                        else:
                            ans += checkCount(tmp, 'G', ji)
                    if Si == 'G':
                        if Sj == 'R':
                            ans += checkCount(tmp, 'B', ji)
                        else:
                            ans += checkCount(tmp, 'R', ji)
                    if Si == 'B':
                        if Sj == 'R':
                            ans += checkCount(tmp, 'G', ji)
                        else:
                            ans += checkCount(tmp, 'R', ji)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(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)
ans = 0
for i in range(len(R)):
    for j in range(len(G)):
        for k in range(len(B)):
            kumi = sorted([R[i],G[j],B[k]])
            if not kumi[1] - kumi[0] == kumi[2] - kumi[1]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
ans = 0
rr,gg,bb = [],[],[]
for i in range(0,len(s)):
    if s[i] == "R":
        rr.append(i)
    elif s[i] == "G":
        gg.append(i)
    else:
        bb.append(i)

for i in range(0,len(rr)):
    for j in range(0,len(gg)):
        che = 0
        a = rr[i] + gg[j]
        if a % 2 == 0:
            if s[a//2] == "B":
                che += -1
        b = min(rr[i],gg[j]) - abs(rr[i]-gg[j])
        if b >= 0:
            if s[b] == "B":
                che += -1
        c = max(rr[i],gg[j]) + abs(rr[i]-gg[j])
        if c < n:
            if s[c] == "B":
                che += -1
        ans += len(bb)
        ans += che

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    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 = 0
    for n in R:
        # x = bisect_left(G, n)
        for i in G:
            a = max(n, i)
            b = min(n, i)
            x = bisect_left(B, a)
            ans += len(B[x:])
            if 2*a-b in B:
                ans -= 1
    for n in R:
        # x = bisect_left(B, n)
        for i in B:
            a = max(n, i)
            b = min(n, i)
            x = bisect_left(G, a)
            ans += len(G[x:])
            if 2*a-b in G:
                ans -= 1
    # for n in G:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(B, i)
    #         ans += len(B[x:])
    #         if 2*i-n in B:
    #             ans -= 1
    for n in G:
        # x = bisect_left(B, n)
        for i in B:
            a = max(n, i)
            b = min(n, i)
            x = bisect_left(R, a)
            ans += len(R[x:])
            if 2*a-b in R:
                ans -= 1
    # for n in B:
    #     x = bisect_left(R, n)
    #     for i in R[x:]:
    #         x = bisect_left(G, i)
    #         ans += len(G[x:])
    #         if 2*i-n in G:
    #             ans -= 1
    # for n in B:
    #     x = bisect_left(G, n)
    #     for i in G[x:]:
    #         x = bisect_left(R, i)
    #         ans += len(R[x:])
    #         if 2*i-n in R:
    #             ans -= 1
    print(ans)
resolve()

Submission status: Time Limit Exceeded

Solution code:
N = input("")
S = input("")
N = int(N)
X = 0

for i in range(N-3):
    j = i+1
    for jj in range(N):
        if not(S[j] == S[i]):
            k = j+1
            for kk in range(N):
                if not(S[k] == S[j] or S[k] == S[i]):
                    if j-i != k-j :
                        X += 1
                k += 1
                if k == N:
                    break            
        j += 1
        if j == N-1:
            break
print(X)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

lis = list(itertools.combinations(range(0, N), 3))

cnt=0
for i, j, k in lis:
    if j-i != k-j:
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=0
for a in range(n-2):
    for b in range(a,n-1):
        for c in range(b,n):
            se=set([a,b,c])
            if len(se)==3:
                if b-a!=c-b:
                    if s[a]!=s[b] and s[b]!=s[c] and s[a]!=s[c]:
                        ans+=1
                    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
r = 0
g = 0
b = 0
for c in s:
    if c == "R":
        r += 1
    if c == "G":
        g += 1
    if c == "B":
        b += 1

ans = r * g * b

p = 1
while p + p < len(s):
    j = 0
    while j + p + p < len(s):
        if s[j] != s[j+p]:
            if s[j+p] != s[j+p+p]:
                if s[j] != s[j+p+p]:
                    ans -= 1
        j += 1
    p += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
c=[[0,0,0] for _ in range(n+1)]
R=G=B=0
for j,i in enumerate(s):
    if i=="R":
        R+=1
    elif i=="G":
        G+=1
    else:B+=1
    c[j+1]=[R,G,B]
a=0
for i,m in enumerate(s):
    for j in range(i):
        k=3-"RGB".index(m)-"RGB".index(s[j])
        if m!=s[j]:
            a+=c[-1][k]-c[i][k]
            if 2*i-j<=n-1:
                if s[2*i-j]=="RGB"[k]:
                    a-=1
print(a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

s = input()

# print(s)
# brute force
rs = 0
gs = 0
bs = 0
for x in s:
    if x == 'R':
        rs += 1
    if x == 'G':
        gs += 1
    if x == 'B':
        bs += 1
# print(rs, gs, bs)

count = 0
for i in range(len(s)):
    for j in range(i, len(s)):
        for k in range(j, len(s)):
            if (s[i] != s[j] and s[j] != s[k] and s[i] != s[k]):
                if j - i != k - j:
                    count += 1
                    # print(s[i], s[j], s[k])

print(count)

Submission status: Time Limit Exceeded

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

res = 0

for k in range(n):
    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]):
                #print(f'({s[i]},{s[j]},{s[k]})')
                res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
cr = S.count('R')
cg = S.count('G')
cb = S.count('B')
ans = cr*cg*cb
#cr*cg*cbからj-i == k-jとなるijkの組みの数を引く
for i in range(N-2):
    for j in range(i+1,N-1):
        num = j-i
        for k in range(j+num,N):
            if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
                if num == k - j:
                    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# Code for D - RGB Triplets
# Use input() to fetch data from STDIN
from collections import defaultdict
d = defaultdict(lambda: [])

n = int(input())
S = input()

for i, s in enumerate(S):
    d[s] = d[s] + [i]

total = len(d['R']) * len(d['G']) * len(d['B'])

sub = 0
for i in range(n):
    for j in range(n - i - 1):
        k = j + i + 1
        if ( k - i ) % 2 != 0:
            continue

        if len(set([S[i], S[int(( k + i ) / 2)], S[k]])) == 3:
            sub += 1
print(total - sub)

Submission status: Time Limit Exceeded

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

r = 0
g = 0
b = 0
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] == "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] == "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] == "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:
import math
n = int(input() )
s = input()



sum = 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):
                    #print(i,j,k)
                    sum+=1

print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

i_range = [int(x) for x in range(1, N-1)]
j_range = [int(x) for x in range(2, N)]
k_range = [int(x) for x in range(3, N+1)]

val = 0


"""
for k in k_range:
    for j in j_range:
        for i in i_range:
            judge = ""
            if( (j-i == k-j) or (i >= j) or (j >= k)):
                print("(i, j, k) = ({}, {}, {}) break".format(i, j, k))
                break
            if((S[i-1] != S[j-1]) and (S[j-1] != S[k-1]) and (S[i-1] != S[k-1])):
                val = val + 1
                judge = "〇"
            else:
                judge = "×"
            print("(i, j, k) = ({}, {}, {}) -> ({}, {}, {}) {}".format(i, j, k, S[i-1], S[j-1], S[k-1], judge))
"""


for k in k_range:
    for j in j_range:
        if(j >= k):
            break
        for i in i_range:
            if(i >= j):
                break
            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):
                    judge = "〇"
                    val = val + 1
                else:
                    judge = "×"
            #print("(i, j, k) = ({}, {}, {}) -> ({}, {}, {}) {}".format(i, j, k, S[i-1], S[j-1], S[k-1], judge))

print(val)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
JK = list()
Ilist = [0]
for k in range(2, N):
    for j in range(1, k):
        if not S[j] == S[k]:
            JK.append((j, k))
            Ilist.append(j)

Iset = set(Ilist)
for jk in JK:
    j, k = jk
    for i in Iset:
        if not i < j:
            break
        if S[i] == S[j]:
            continue
        if S[i] == S[k]:
            continue
        if j-i == k-j:
            continue
        ans += 1
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):
        for k in range(j+1, N):
            if (j - i) != (k - j):
                if S[i] == S[j] or S[j] == S[k] or S[k] == S[i]:
                    continue
                else:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
All = [i for i in range(1,N+1)]
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)
ans = 0
count = 0
for k in R:
    for j in G:
        for i in B:
            fr = 0
            ab = [k,j,i]
            ab.sort()
            if ab[2] - ab[1] == ab[1] - ab[0]:
                fr += 1

            if fr == 0:
                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)
    elif S[i] == "G":
        G.append(i)
    else:
        B.append(i)
s = 0
for i in R:
    for j in G:
        for k in B:
            if abs(i-j) != abs(j-k) and abs(i-j) != abs(k-i) and abs(j-k) != abs(k-i):
                s += 1
print(s)

Submission status: Time Limit Exceeded

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

check = dict()
def get_pattern2(i, j):
    a = s[i]
    b = s[j]
    total = 0
    check_str = ""
    if a == "R" or b == "R":
        check_str += "R"
    if a == "G" or b == "G":
        check_str += "G"
    if a == "B" or b == "B":
        check_str += "B"

    center = j - i
    if n < j + 1 + center:
        center = 0

    for k in range(j+1+center, n):
        if check_str+str(k) in check:
            total += check[check_str+str(k)]
            break
        if a != s[k] and b != s[k]:
            total += 1
    check[check_str+str(j+1+center)] = total

    for k in range(j+1, j+center):
        if a != s[k] and b != s[k]:
            total += 1
    return total

def get_pattern(i):
    a = s[i]
    total = 0
    for j in range(i+1, n):
        if a != s[j]:
            total += get_pattern2(i, j)
    return total

total = 0
for i in range(n-1, -1, -1):
    total += get_pattern(i)
print(total)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
a = s.count("R")
b = s.count("G")
c = s.count("B")
x = 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], s[k]}=={"R", "G", "B"} and i+k == 2*j:
        x += 1
print(a*b*c-x)

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
        rgb_list = ['R', 'G', 'B']
        rgb_list.remove(a)
        rgb_list.remove(b)
        # current_list = [a, b]
        # diff_list = list(set(rgb_list) - set(current_list))
        c = rgb_list[0]
        nokori = s[p + q:]
        counter += nokori.count(c)
        if q < len(nokori) and nokori[q] == c:
            counter -= 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())
S = input()
counter = {'R': [0], 'G': [0], 'B': [0]}
for s in S:
    for c in 'RGB':
        counter[c].append(counter[c][-1] + (1 if s == c else 0))
ans = 0
for i, j in combinations(range(N-1), 2):
    if S[i] == S[j]: continue
    l = j - i
    temp = set([S[i], S[j]])
    if 'R' not in temp: key = 'R'
    elif 'G' not in temp: key = 'G'
    else: key = 'B'
    cnt = counter[key]
    if j + l > N:
        ans += cnt[-1] - cnt[j]
    else:
        ans += cnt[j+l] - cnt[j] + (cnt[-1] - cnt[j + l + 1] if j + l + 1 <= N else 0)
print(ans)