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:
import math
def main():
    #N,K,C=map(int,input().split())
    #A=list(map(int,input().split()))
    N=input()
    S=input()
    N=int(N)
    ans=0
    ind=[]
    tmp={"R":0,"G":0,"B":0}
    for i in S:
        tmp[i]+=1
        ind.append(tmp.copy())
    #print(ind)
    for i in range(0,N-2):
        for j in range(i+1,N-1):
            if S[i]==S[j]:
                continue
            for k in tmp.keys():
                if k!=S[i] and k!=S[j]:
                    col=k
                    break
            #print(i,j,col)
            if 2*j-i <N and S[j+j-i]==col:
                ans-=1
            ans+=(ind[-1][col]-ind[j][col])
    print(ans)

main()

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(N):
    for j in range(i,N):
        for k in range(j,N):
            if j-i==k-j:
                continue
            else:
                if S[i]==S[j] or S[i]==S[k] or S[k]==S[j]:
                    continue
                else:
                    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)
ans = len(r) * len(g) * len(b)
for a in r:
  for k in g:
    for l in b:
      ze = abs(a-k)
      m = abs(k-l)
      n = abs(l-a)
      if ze == m or m == n or n == ze:
        ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
ans = 0
for i in range(0,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] 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(0,N-2):
    for j in range(i+1,N-1):
        if S[i] == S[j]:
            continue
        else:
            for k in range(j+1,N):
                if j-i == k-j:
                    continue
                else:
                    if S[i] != S[k] and S[j] != S[k]:
                        ans = ans + 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N=int(input())
S=list(input())
RGB=[[0,0,0] for i in range(N)]

ans=0

for i in range(1,N):
    k=-i
    if S[k]=="R":
        RGB[k-1][0]=RGB[k][0]+1
        RGB[k-1][1]=RGB[k][1]
        RGB[k-1][2]=RGB[k][2]
    elif S[k]=="G":
        RGB[k-1][0]=RGB[k][0]
        RGB[k-1][1]=RGB[k][1]+1
        RGB[k-1][2]=RGB[k][2]
    elif S[k]=="B":
        RGB[k-1][0]=RGB[k][0]
        RGB[k-1][1]=RGB[k][1]
        RGB[k-1][2]=RGB[k][2]+1

for i in range(N):
    for j in range(i,N):
        if S[i]!=S[j]:
            if S[i]=="R" and S[j]=="G" or S[i]=="G" and S[j]=="R":
                ans+=RGB[j][2]
                if 2*j-i<N:
                    if RGB[j][2]>0 and S[2*j-i]=="B":
                        ans-=1
            elif S[i]=="R" and S[j]=="B" or S[i]=="B" and S[j]=="R":
                ans+=RGB[j][1]
                if 2*j-i<N:
                    if RGB[j][1]>0 and S[2*j-i]=="G":
                        ans-=1
            elif S[i]=="B" and S[j]=="G" or S[i]=="G" and S[j]=="B":
                ans+=RGB[j][0]
                if 2*j-i<N:
                    if RGB[j][0]>0 and S[2*j-i]=="R":
                        ans-=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import product as pro


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

r = [i for i in range(n) if s[i] == 'R']
g = [i for i in range(n) if s[i] == 'G']
b = [i for i in range(n) if s[i] == 'B']
cnt = 0

for i,j,k in pro(r,g,b):
    num = [i, j, k]
    num.sort()
    if num[1]-num[0]==num[2]-num[1]:
        continue
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

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)

kouho = list(itertools.product(R, G, B))
ans = 0
for i in range(len(kouho)):
  temp = [kouho[i][0], kouho[i][1], kouho[i][2]]
  temp.sort()
  if temp[1] - temp[0] != temp[2] - temp[1]:
    ans += 1

print(ans)

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']
R,G,B=len(r),len(g),len(b)
ans=0
for i in range(R):
    for j in range(G):
        for k in range(B):
            l=sorted([r[i],g[j],b[k]])
            ans+=1 if l[1]-l[0]!=l[2]-l[1]  else 0
print(ans)

Submission status: Time Limit Exceeded

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

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

cnt = 0

if N < 3:
    print(0)
    exit

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

print(cnt)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(0, N-2):
    S_i = S[i]
    for j in range(i+1, N-1):
        S_j = S[j]
        if S_i != S_j:
            for k in range(j+1, N):
                S_k = S[k]
                if S_j != S_k and S_k != S_i and j-i != k-j:
                    count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)  # 再帰関数の再帰の深さを設定
to_index = lambda x: int(x) - 1  # 入力した数字に1を引いたものを返す
print_list_in_2D = lambda x: print(*x, sep="\n")  # リストの要素を改行を挟んで表示する関数

# 入力を整数に変換して受け取る
def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

def MII_split():
    return map(int, input().split())

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())

# 入力全てを整数に変換したものの配列を受け取る
def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

def LII_split():
    return list(map(int, input().split()))

# 2次元リスト化
def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

N = input_int()
S = input()

if N < 3:
    print(0)
    exit()
count = 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[j] == S[k] or S[i] == S[k] or j - i == k - j:
                continue
            else:
                count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
if n<=3:
    print(0)
    exit()
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[i]!=s[k] and s[j]!=s[k]:
                if j-i!=k-j:
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n=int(input())
    s=input()
    ans=0
    for i in range(n):
        for j in range(i,n):
            for k in range(j,n):
                if {s[i],s[j],s[k]}=={"R","G","B"} and j-i!=k-j:
                    ans+=1
    print(ans)
resolve()

Submission status: Time Limit Exceeded

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input().strip())
S = input().strip()
res = 0
for i in range(N):
    for j in range(i,N):
        for k in range(j,N):
            if j-i != k-j:
                if len(set([S[i],S[j],S[k]]))==3:
                    res += 1
print(res)

Submission status: Time Limit Exceeded

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

for i in range(N-3):
    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] and j - i != k - j:
                n = n+1

print(n)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = set()
B = set()
G = set()
for i in range(N):
    if S[i] == "R":
        R.add(i)
    elif S[i] == "B":
        B.add(i)
    elif S[i] == "G":
        G.add(i)
if len(R) < len(B) and len(B) > len(G):
    B,G = G,B
elif len(R) > len(B) and len(R) > len(G):
    R,G = G,R


c = 0
for r in R:
    for b in B:
        p = set()
        if r < b:
            p.add(r - (b-r))
            if ((r + b)/2).is_integer():
                p.add((r + b) // 2)
            p.add(b + (b-r))
        elif r > b:
            p.add(b - (r-b))
            if ((r + b)/2).is_integer():
                p.add((r + b) // 2)
            p.add(r + (r-b))

        c += len(G - p)
print(c)

Submission status: Time Limit Exceeded

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

def rule1(i, j, k, S):
    if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
        return True
    else:
        return False

def rule2(i, j, k):
    if j-i != k-j:
        return True
    else:
        return False

def flag_make(x, S):
    if S[x] == 'R':
        flag_list[0] = True
    elif S[x] == 'G':
        flag_list[1] = True
    else:
        flag_list[2] = True

def flag_check(x, S):
    if S[x] == 'R':
        if flag_list[0]:
            return False
        else:
            return True
    elif S[x] == 'G':
        if flag_list[1]:
            return False
        else:
            return True
    else:
        if flag_list[2]:
            return False
        else:
            return True

def flag_clear(x, S):
    if S[x] == 'R':
        flag_list[0] = False
    elif S[x] == 'G':
        flag_list[1] = False
    else:
        flag_list[2] = False


result = 0
flag_list = [False, False, False] # RGB
for i in range(N):
    flag_make(i, S)
    for j in range(i, N):
        if flag_check(j, S):
            flag_make(j, S)
        else:
            continue
        for k in range(j, N):
            if flag_check(k, S):
                pass
            else:
                continue
            if rule1(i, j, k, S) and rule2(i, j, k):
                result += 1
        flag_clear(j, S)
    flag_clear(i, S)

print(result)

Submission status: Time Limit Exceeded

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

    c = 0

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

    print(c)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=0
for i in range(N-2):
  for j in range(i+1,N-1):
    for k in range(j+1,N):
      if j-i!=k-j:
        if S[i]!=S[j] and S[j]!=S[k] and S[i]!=S[k]:
          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
for i in range(n-2):
    t = i+1
    while 2*t-i <= n-1:
        if set((s[i],s[t],s[2*t-i])) == set(('R','G','B')):
            res -= 1
        t += 1
print(res)

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] and s[j] != s[k] and s[k] != s[i] and j-i != k-j:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
cnt = 0
for i in range(n):
    for j in range(n):
        if j <= i:
            continue
        for k in range(n):
            if k <= j:
                continue
            if s[i] != s[j] and 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:
N=int(input())
S=input()
count=0
for i in range(N):
    for j in range(N):
        for k in range(N):
            if i<j and j<k and(j-i)!=(k-j) 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:
N = int(input())
S = list(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[j] and S[j] != S[k] and S[i] != S[k] and j-i != k-j:
                    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

#i文字目以降のRGBの数
RGB = [[0]*(N+1) for i in range(3)]
for i in range(N-1,-1,-1):
    if S[i] == "R":
        RGB[0][i] = RGB[0][i+1]+1
        RGB[1][i] = RGB[1][i+1]
        RGB[2][i] = RGB[2][i+1]
    elif S[i] == "G":
        RGB[0][i] = RGB[0][i+1]
        RGB[1][i] = RGB[1][i+1]+1
        RGB[2][i] = RGB[2][i+1]
    else:
        RGB[0][i] = RGB[0][i+1]
        RGB[1][i] = RGB[1][i+1]
        RGB[2][i] = RGB[2][i+1]+1

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        if (S[i] == "R" and S[j] == "G") or (S[i] == "G" and S[j] == "R"):
            ans += RGB[2][j]
            if j+(j-i) < N and S[j+(j-i)] == "B":
                ans -= 1
        elif (S[i] == "G" and S[j] == "B") or (S[i] == "B" and S[j] == "G"):
            ans += RGB[0][j]
            if j+(j-i) < N and S[j+(j-i)] == "R":
                ans -= 1
        elif (S[i] == "R" and S[j] == "B") or (S[i] == "B" and S[j] == "R"):
            ans += RGB[1][j]
            if j+(j-i) < N and S[j+(j-i)] == "G":
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lis = str(input())
r = []
g = []
b = []
for i in range(n):
  if lis[i] == "R":
    r.append(i)
  elif lis[i] == "G":
    g.append(i)
  else:
    b.append(i)
ans = len(r) * len(g) * len(b)
for i in range(1,n-1):
  for j in range(i+1,n):
    if i != j and 2 * j -i <= n:
      k = j + (j-i)
      if (lis[i-1] != lis[j-1]) and (lis[j-1] != lis[k-1]) and (lis[k-1] != lis[i-1]):
        ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import permutations,combinations
from collections import Counter
from pprint import pprint

def myinput():
    return map(int,input().split())

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col):
    data.sort(key=lambda x:x[col],reverse=False)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

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)
    elif s[i]=="B":
        b.append(i)
    else:
        pass
# print(r,g,b)

def mycount(r,g,b):
    c = 0
    for i in r:
        ng = [ x for x in g if x>i ]
        for j in ng:
            nb = [ y for y in b if y>j ]
            for k in nb:
                if (j-i)!=(k-j):
                    c += 1
                else:
                    pass
    return c

ans = 0
ans += mycount(r,g,b)
ans += mycount(r,b,g)
ans += mycount(g,r,b)
ans += mycount(g,b,r)
ans += mycount(b,r,g)
ans += mycount(b,g,r)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
r=[]
r2=[]
g=[]
g2=[]
b=[]
b2=[]
for i in range(n):
  if s[i]=='R':
    r.append(i)
    r2.append(i*2)
  elif s[i]=='G':
    g.append(i)
    g2.append(i*2)
  else:
    b.append(i)
    b2.append(i*2)
ans=len(r)*len(g)*len(b)
for i in g:
  for j in b:
    if i+j in r2:
      ans-=1
for i in r:
  for j in b:
    if i+j in g2:
      ans-=1
for i in g:
  for j in r:
    if i+j in b2:
      ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

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




print(count)

Submission status: Time Limit Exceeded

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

count = 0

for i in range(n-2):
    for j in range(i+1, n):
        for k in range(j+1, n):
            if j-i == k-j:
                pass
            else:
                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():
    from itertools import accumulate

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

    R, G, B = [0], [0], [0]
    for s in S:
        if s == 'R':
            R.append(1)
            G.append(0)
            B.append(0)

        elif s == 'G':
            R.append(0)
            G.append(1)
            B.append(0)

        elif s == 'B':
            R.append(0)
            G.append(0)
            B.append(1)

    R = list(accumulate(R))
    G = list(accumulate(G))
    B = list(accumulate(B))

    # print(R)
    # print(G)
    # print(B)
    ans = 0
    for j in range(1, N - 1):
        sj = S[j]
        for i in range(j):
            si = S[i]
            if si == sj:
                continue
            # print(sj, j, i)
            if set(['R', 'G', 'B']) - {sj} - {si} == {'R'}:
                ans += R[-1] - R[j + 1]
                if (j + j - i < N) and si != S[j + j - i] and sj != S[j + j - i]:
                    ans -= 1
                # print('Rx', ans)

            if set(['R', 'G', 'B']) - {sj} - {si} == {'G'}:
                ans += G[-1] - G[j + 1]

                if (j + j - i < N) and si != S[j + j - i] and sj != S[j + j - i]:
                    ans -= 1
                # print('Gx', ans)

            if set(['R', 'G', 'B']) - {sj} - {si} == {'B'}:
                ans += B[-1] - B[j + 1]
                if (j + j - i < N) and si != S[j + j - i] and sj != S[j + j - i]:
                    ans -= 1
                # print('Bx', ans)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
B = {'R','G','B'}
ans = 0
for i in range(N-1):
    s = S[i]
    for j in range(i+1,N):
        if S[j] != s:
            l = j-i
            t = (B-{s,S[j]}).pop()
            L = S[j+1:j+l]
            R = S[j+l+1:]
            ans += L.count(t)+R.count(t)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
 
ans = 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[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 = input()
S = list(s[i] for i in range(len(s)))
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(x)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
dic={"R":0,"G":1,"B":2}
from copy import deepcopy as dc
l=[0 for i in range(n)]
r,g,b=0,0,0
ans=0
for i in range(n):
    if s[i]=="R":r+=1
    elif s[i]=="G":g+=1
    else:b+=1
    l[i]=[r,g,b]
for i in range(n):
    for j in range(i+1,n):
        if s[i]!=s[j]:
            ans+=l[j][3-dic[s[j]]-dic[s[i]]]-l[i][3-dic[s[j]]-dic[s[i]]]
            if (i+j)%2==0 and s[(i+j)//2]=="RGB"[3-dic[s[j]]-dic[s[i]]]:ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
r=[0]*n
b=[0]*n
g=[0]*n
cb=0
cg=0
cr=0
for i in range(n-1,-1,-1):
  if s[i]=='R':
    cr+=1
  elif s[i]=='G':
    cg+=1
  else:
    cb+=1
  r[i]=cr
  g[i]=cg
  b[i]=cb
p=0
for i in range(n):
  for j in range(i+1,n-1):
    if( (s[i]=='R' and s[j]=='B') or (s[i]=='B' and s[j]=='R') ):
      p+=g[j+1]
      if j+(j-i)<n and s[j+(j-i)]=='G':
        p-=1
        
    if( (s[i]=='R' and s[j]=='G') or (s[i]=='G' and s[j]=='R') ):
      p+=b[j+1]
      if j+(j-i)<n and s[j+(j-i)]=='B':
        p-=1
     
    if( (s[i]=='G' and s[j]=='B') or (s[i]=='B' and s[j]=='G') ):
      p+=r[j+1]
      if j+(j-i)<n and s[j+(j-i)]=='R':
        p-=1
print(p)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

dict={'R':1,'G':2,'B':3}
num={1:'R',2:'G',3:'B'}

snum=[[0,0,0] for i in range(n)]
for i in range(n):
    if i == 0:
        snum[0][dict[s[i]]-1] = 1
        continue
    for j in range(3):
        if dict[s[i]]-1 == j:
            snum[i][j] = snum[i-1][j] + 1
        else:
            snum[i][j] = snum[i-1][j]
#print(snum)
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        temp=s[i]
        temp1=s[j]
        if temp == temp1:continue

        temp_num=6-dict[temp]-dict[temp1]
        temp2=num[temp_num]
        count=snum[-1][temp_num-1]-snum[j][temp_num-1]
        #print(count)
        if 2*j-i<n and s[2*j-i] == temp2:
            count-=1
        ans+=count
print(ans)

Submission status: Time Limit Exceeded

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

r_list = []
g_list = []
b_list = []
for i, s in enumerate(S):
    if s == 'R':
        r_list.append(i)
    elif s == 'G':
        g_list.append(i)
    elif s == 'B':
        b_list.append(i)

count = 0
for a in r_list:
    for b in g_list:
        for c in b_list:
            i, j, k = sorted((a, b, c))
            if j - i != k - j:
                count += 1
print(count)

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 + 1)
    elif S[i] == 'G':
        G.append(i + 1)
    else:
        B.append(i + 1)

ans = 0
for r in R:
    for g in G:
        for b in B:
            if abs(r - g) == abs(r - b):
                pass
            elif abs(r - g) == abs(g - b):
                pass
            elif abs(r - b) == abs(g - b):
                pass
            else:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
sum = 0
for i in range(N):
    for j in range(i):
        for k in range(j):
            if(k - j == j - i):
                continue
            elif(i == j):
                continue
            elif(j == k):
                continue
            elif(k == i):
                continue
            if(S[i] == S[j]):
                continue
            elif(S[j] == S[k]):
                continue
            elif(S[i] == S[k]):
                continue
            sum += 1
            
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import heapq
from collections import defaultdict, deque
from math import ceil, factorial, gcd
import sys

sys.setrecursionlimit(10 ** 7)
INF = float("inf")
MOD = 10 ** 9 + 7

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
smii = lambda: sorted(map(int, input().split()))


N = ii()
S = si()

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)

colors = [
    [R, G, B],
    [R, B, G],
    [G, R, B],
    [G, B, R],
    [B, R, G],
    [B, G, R],
]

ans = 0

for C1, C2, C3 in colors:
    for c1 in C1:
        for c2 in C2:
            if c1 > c2:
                continue
            for c3 in C3:
                if c2 > c3:
                    continue
                if c1 > c3:
                    continue
                if c2 - c1 != c3 - c2:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

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

Rs = []
Gs = []
Bs = []
for index, s in enu(S):
    if s == 'R':
        Rs.append(index+1)
    elif s == 'G':
        Gs.append(index+1)
    elif s == 'B':
        Bs.append(index+1)
#print(Rs, Gs, Bs)

cnt = 0
for r in Rs:
    for g in Gs:
        for b in Bs:
            i, j, k = sorted([r, g, b])
            #print(' ', i, j, k)
            if j-i != k-j:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

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

val = 0

for i in range(n):
    if s[i] == "R":
        r.append(i+1)
    elif s[i] == "G":
        g.append(i+1)
    else:
        b.append(i+1)
for i in r:
    for j in g:
        for k in b:
            if j - i == k - j or k - j == i - k or i - k == j - i:
                val +=1
print(len(r) * len(g) * len(b) - val)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
count = 0
for i in range(n-2):
    l = ["R", "G", "B"]
    l.remove(s[i])
    for j in range(i+1,n-1):
        if s[i] == s[j]:
            continue
        x = j-i
        l.remove(s[j])
        for k in range(j+1,n):
            if x == k-j:
                continue
            if s[k] in l:
                count += 1
        l.append(s[j])
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(sys.stdin.readline())
s = sys.stdin.readline()
r = []
g = []
b = []
cnt = 0
i = 0
while i < n:
  if s[i] == "R":
    r.append(i)
  elif s[i] == "G":
    g.append(i)
  else:
    b.append(i)
for j in r:
  for k in g:
    for l in b:
      if l - k != k - j:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
SS=[a for a in S]
SS.insert(0,0)
answer=0
for i in range(1,N-1):
    for j in range(i+1,N+1):
        if SS[i]==SS[j]:
            continue
        for k in range(j+1,N+1):
            if i+k==j*2:
                continue
            else:
                if SS[i]!=SS[j] and SS[j]!=SS[k] and SS[k]!=SS[i]:
                    answer+=1
print(answer)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
pairs = []
for i, c1 in enumerate(S):
    for j, c2 in enumerate(S[i + 1:], start=i + 1):
        if c1 != c2:
            pairs.append((i, j))
res = 0
for i, j in pairs:
    for k, c3 in enumerate(S[j + 1:], start=j + 1):
        if j - i == k - j:
            continue
        elif S[j] == c3 or S[i] == c3:
            continue
        else:
            res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
inputs = open(0).readlines()
n = int(inputs[0])
S = inputs[1].strip()

def is_cond(i, j, k):
    return j-i != k-j

l = []
for i in range(n-2):
    rest = {'R', 'G', 'B'}
    rest.remove(S[i])
    for j in range(i+1, n-1):
        if S[j] in rest:
            rest.remove(S[j])
            for k in range(j+1, n):
                if S[k] in rest:
                    if is_cond(i, j, k):
                        l.append((i, j, k))
            rest.add(S[j])
print(len(l))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = []
nsum = 0
for i in range(N):
    if S[i] == "R":
        R.append(i)
B = []
for i in range(N):
    if S[i] == "B":
        B.append(i)
g = 0
for i in range(N):
    if S[i] == "G":
        g += 1
        for r in R:
            for b in B:
                if abs(i - b) - abs(b - r) == 0 or abs(b - i) - abs(r - i) == 0 or abs(b - r) - abs(i - r) == 0:
                    nsum += 1
print(len(R)*len(B)*g - nsum)

Submission status: Time Limit Exceeded

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

ret = 0

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

print(ret)

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, N-1):
        I = S[i]
        J = S[j]
        if I != J:
            color = ['R', 'G', 'B']
            color.remove(I)
            color.remove(J)
            ans += S[j+1:].count(color[0])
            try:
                if S[j+(j-i)] == color[0]:
                    ans -= 1
            except IndexError:
                continue
print(ans)