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()
ans = S.count("R") * S.count("G") * S.count("B")

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

print(ans)

Submission status: Time Limit Exceeded

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

a = 0

for i in range(n):
  if s[i]=='R':
    for j in range(n):
      if s[j]=='G':
        for k in range(n):
          if s[k]=='B':
            if i<j<k:
              if (j-i)!=(k-j):
                a = a + 1
            elif j<k<i:
              if (k-j)!=(i-k):
                a = a + 1
            elif k<i<j:
              if abs(j-i)!=abs(k-i):
                a = a + 1
            elif j<i<k:
              if abs(j-i)!=abs(k-i):
                a = a + 1
            elif i<k<j:
              if abs(k-i)!=abs(k-j):
                a = a + 1
            elif k<j<i:
              if abs(j-i)!=abs(k-j):
                a = a + 1
          
print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = input()

index = defaultdict(list)

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

G = sorted(index["G"])
R = sorted(index["R"])
B = sorted(index["B"])

count = 0
for r in R:
    for g in G:
        for b in B:
            s = min(r, g, b)
            l = max(r, g, b)
            if r != s and r != l:
                m = r
            if g != s and g != l:
                m = g
            if b != s and b != l:
                m = b
            if m - s != l - m:
                count += 1
            

print(count)

Submission status: Time Limit Exceeded

Solution code:
#problem D
input_d41 = int(input())
input_d42 = input()

N = int(input_d41)
S = input_d42

R = S.count('R')
G = S.count('G')
B = N - (R + G)

ans = 0
answ = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

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

num_list = []
r, g, b = 0, 0, 0
for i in range(n-1, -1, -1):
    if s[i] == "R":
        r += 1
    elif s[i] == "G":
        g += 1
    elif s[i] == "B":
        b += 1
    num_list.append([r, g, b])

num_list.reverse()

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        rest_str = "B"
        dev = [s[i], s[j]]
        dev.sort()
        if ["B", "G"] == dev:
            rest_num = 0
            rest_str = "R"
        elif ["B", "R"] == dev:
            rest_num = 1
            rest_str = "G"
        elif ["G", "R"] == dev:
            rest_num = 2
            rest_str = "B"

        ans += num_list[j+1][rest_num]
        if j+j-i < n:
            if s[j+j-i] == rest_str:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import product

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

temp_dict = {
    "R": [],
    "G": [],
    "B": [],
}
for i, c in enumerate(s):
    temp_dict[c].append(i)
# print(temp_dict)

ans = 0
for temp in list(product(temp_dict["R"], temp_dict["G"], temp_dict["B"])):
    temp = sorted(list(temp))
    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())
import itertools as itr

A = {'R':[] ,'G':[] ,'B':[]}
sum = 0

for i in range(N):
    A[S[i]].append(i+1)



for i,j,k in itr.product(A['R'],A['B'],A['G']):
            if not i-j == k-j:
                l = [i,j,k]
                l.sort()
                if not l[1] - l[0] == l[2] - l[1]:
                    sum += 1

print(sum)

Submission status: Time Limit Exceeded

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

cnt=0
r = []
g = []
b = []
for i, x in enumerate(s):
    if x == "R":
        r.append(i)
    elif x == "G":
        g.append(i)
    else:
        b.append(i)


for i in r:
    for j in g:
        if j>i:
            for k in b:
                if k>j:
                    if j-i!=k-j:
                        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                            cnt += 1
for i in r:
    for j in b:
        if j>i:
            for k in g:
                if k>j:
                    if j-i!=k-j:
                        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                            cnt += 1
for i in g:
    for j in r:
        if j>i:
            for k in b:
                if k>j:
                    if j-i!=k-j:
                        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                            cnt += 1
for i in g:
    for j in b:
        if j>i:
            for k in r:
                if k>j:
                    if j-i!=k-j:
                        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                            cnt += 1
for i in b:
    for j in r:
        if j>i:
            for k in g:
                if k>j:
                    if j-i!=k-j:
                        if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                            cnt += 1
for i in b:
    for j in g:
        if j>i:
            for k in r:
                if k>j:
                    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:
from collections import deque
n = int(input())
S = list(input())
q = deque(S)
ans = 0
for i in range(n-2):
  c = q.popleft()
  for j in range(n-i-2):
    s = list(q)
    if c == s[j]:
      continue
    a = j+1
    for k in range(j+1, n-i-1):
      b = k-j
      if a == b:
        continue
      elif s[k] != c and s[k] != s[j]:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from random import sample
n = int(input())
s = input()
list1=[]
j = 0
for i in range(n):
    list1.append(i)
while True:
    list2 = (sample(list1,3))
    if s[list2[0]] != s[list2[1]] and s[list2[0]] != s[list2[2]] and list2[1] -list2 [0] != list2[2] -list2[1]:
        j = j + 1
print(j)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
S = list(input())
S_indexr = [int(i+1) for i, x in enumerate(S) if x == 'R']
S_indexg = [int(i+1) for i, x in enumerate(S) if x == 'G']
S_indexb = [int(i+1) for i, x in enumerate(S) if x == 'B']
ans = 0
for i in S_indexr:
  for j in S_indexg:
    for k in S_indexb:
      tmp = sorted([i,j,k])
      i_ = tmp[0]
      j_ = tmp[1]
      k_ = tmp[2]
      if k_-j_ != j_-i_:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

counta = 0
i = 0

while i < N - 2:
    j = i + 1
    while j < N - 1:
        if S[j] != S[i]:
            k = j + 1
            while k < N:
                if k - j != j - i and S[k] != S[i] and S[k] != S[j]:
                    counta += 1
                k += 1
        j += 1
    i += 1

print(counta)

Submission status: Time Limit Exceeded

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

total = n * (n-1) * (n-2)

cnt = 0

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

print(cnt)

Submission status: Time Limit Exceeded

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


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

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
n=int(input())
s=input()
r=[]
g=[]
b=[]
lr=0
lg=0
lb=0
for i in range(n):
    if(s[i]=='R'):
        r.append(i+1)
        lr+=1
    elif(s[i]=='G'):
        g.append(i+1)
        lg+=1
    else:
        b.append(i+1)
        lb+=1
ans=0
# print(r,g,b)
# print(lr,lg,lb)
for i in range(lr):
    for j in range(lg):
        ind1=bisect(b,(r[i]+g[j])/2)
        ind2=bisect(b,2*g[j]-r[i])
        ind3=bisect(b,2*r[i]-g[j])
        if(ind1!=lb and (b[ind1]==(r[i]+g[j])/2)) or (ind1!=0 and (b[ind1-1]==(r[i]+g[j])/2)) :
            ans+=1
        if(ind2!=lb and (b[ind2]==(2*g[j]-r[i]))) or (ind2!=0 and b[ind2-1]==(2*g[j]-r[i])):
            ans+=1
        if(ind3!=lb and (b[ind3]==(2*r[i]-g[j]))) or(ind3!=0 and b[ind3-1]==(2*r[i]-g[j])):
            ans+=1
        # print(ind1,ind2,ind3)
# print(ans)
print(lr*lb*lg-ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline
 
 
def main():
    n = int(input())
    s = list(input())[:n]
 
    # memory = [[0 for j in range(n)] for i in range(n)]
    # for i in range(n):
    #     for j in range(i, n):
    #         memory[i][j] = s[i] == s[j]
    #         memory[j][i] = s[i] == s[j]
    # print(memory)
 
    result = 0
    for i in range(n):
        for j in range(i, n):
            for k in range(j, n):
                if s[i] == s[j] or s[j] == s[k] or s[k] == s[i]:
                    continue
                if j - i == k - j:
                    continue
                result += 1
    print(result)
 
 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n = int(input())
    s = str(input())
    s_size = n + 1
    count = 0
    for i in range(1, s_size):
        for j in range(i, s_size):
            for k in range(j, s_size):
                if 1 <= i < j < k <= n:
                    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:
                            count += 1
    print(count)

resolve()

Submission status: Time Limit Exceeded

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

def abc162d(n, s):

    r_arr = []
    g_arr = []
    b_arr = []

    for i in range(n):
        if s[i] == 'R':
            r_arr.append(i)
        elif s[i] == 'G':
            g_arr.append(i)
        else: # 'B'
            b_arr.append(i)

    min1_arr = r_arr
    min2_arr = g_arr
    max_arr = b_arr
    if len(r_arr) < len(g_arr) and len(b_arr) < len(g_arr):
        min1_arr = r_arr
        min2_arr = b_arr
        max_arr = g_arr
    elif len(g_arr) < len(r_arr) and len(b_arr) < len(r_arr):
        min1_arr = g_arr
        min2_arr = b_arr
        max_arr = r_arr

    ans = 0
    for r_i in min1_arr:
        for g_i in min2_arr:
            min_i = r_i
            max_i = g_i
            if g_i < r_i:
                min_i = g_i
                max_i = r_i
            diff = max_i - min_i
            minmin = min_i - diff
            maxmax = max_i + diff
            exists_central = (max_i + min_i) % 2
            central = (max_i + min_i) / 2

            ans += len(max_arr)

            if minmin in max_arr:
                ans -= 1
            if maxmax in max_arr:
                ans -= 1
            if exists_central == 0:
                if central in max_arr:
                    ans -= 1

    return ans

###
# main
if(__name__ == '__main__'):
    # input
    n = int(input())
    s = input()
    
    ans = abc162d(n, s)
    
    # output
    print(ans)
# else:
    # do nothing

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(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)
def rgb(x,y,z):
    l = []
    d = []
    for i in x:
        for j in y:
            if j > i:
                l.append(j)
                d.append(j - i)
    ans = 0
    for i in z:
        for j in range(len(l)):
            if i > l[j]:
                if d[j] != (i - l[j]):
                    ans += 1
    return ans
ans = 0
ans += rgb(r,g,b)
ans += rgb(r,b,g)
ans += rgb(g,r,b)
ans += rgb(g,b,r)
ans += rgb(b,r,g)
ans += rgb(b,g,r)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def ss(rr, gg, bb):
    i, j, k = tuple(sorted((rr, gg, bb)))
    return j - i != k - j

n = int(input())
s = input()
r = list()
g = list()
b = list()
for i in range(0, len(s)):
    v = s[i]
    if v == "R":
        r.append(i)
    elif v == "G":
        g.append(i)
    else:
        b.append(i)
c = 0
for rr in r:
    for gg in g:
        for bb in b:
            if ss(rr, gg, bb):
                c += 1

print(c)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = list(input())
ans = 0
for a in range(0,N-2):
    for  b in range(a+1,N-1,1):
        if s[a]!=s[b]:
            RGBlist = ["R","G","B"]
            RGBlist.remove(s[a])
            RGBlist.remove(s[b])
            ans_list = s[(b+1):]
            if b-a <= N-b-1:
                ans_list.pop(b-a-1)
            ans += ans_list.count(RGBlist[0])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.buffer.readline
s =int(readline().rstrip().decode())
t = readline().rstrip().decode()
res = 0
for i in range(s):
    for j in range(s-i):
        for k  in range(s-i-j):
          if t[i] != t[j+i] and t[j+i] != t[k+i+j] and t[i] !=t[k+i+j] and j != k:
            res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
r = [i + 1 for i in range(n) if s[i] == 'R']
g = [i + 1 for i in range(n) if s[i] == 'G']
b = [i + 1 for i in range(n) if s[i] == 'B']

def count(x,y,z):
    c = 0
    for i in x:
        for j in y:
            if i > j: continue 
            for k in z:
                if j > k: continue 
                if j - i != k - j:
                    c += 1
    return c

ans = 0
ans += count(r,g,b)
ans += count(r,b,g)
ans += count(b,r,g)
ans += count(b,g,r)
ans += count(g,r,b)
ans += count(g,b,r)

print(ans)

Submission status: Time Limit Exceeded

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

    su = 0
    for i, si in enumerate(S[:-2]):
        for j, sj in enumerate(S[i+1:-1]):
            if si == sj:
                continue
            tmps = S[i+j+2:]
            if len(tmps) > j:
                if tmps[j] not in [sj, si]:
                    su -= 1

            tmps2 = [s for s in tmps if s not in [si, sj]]
            su += len(tmps2)
    print(su)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

count = 0

for i in range(n):
    if s[i] == 'R':
        for j in range(i+1, n):
            if s[j] == 'G':
                d = j - i
                for k in range(j+1, n):
                    if s[k] == 'B' and k - j != d:
                        count += 1
            elif s[j] == 'B':
                d = j - i
                for k in range(j+1, n):
                    if s[k] == 'G' and k - j != d:
                        count += 1

    elif s[i] == 'G':
        for j in range(i+1, n):
            if s[j] == 'R':
                d = j - i
                for k in range(j+1, n):
                    if s[k] == 'B' and k - j != d:
                        count += 1
            elif s[j] == 'B':
                d = j - i
                for k in range(j+1, n):
                    if s[k] == 'R' and k - j != d:
                        count += 1

    elif s[i] == 'B':
        for j in range(i+1, n):
            if s[j] == 'G':
                d = j - i
                for k in range(j+1, n):
                    if s[k] == 'R' and k - j != d:
                        count += 1
            elif s[j] == 'R':
                d = j - i
                for k in range(j+1, n):
                    if s[k] == 'G' and k - j != d:
                        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import copy
N = int(input())
S = input()
ans = 0
R = []
G = []
B = []
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
    R.append(r)
    G.append(g)
    B.append(b)

for i in range(N-3):
    for k in range(i+3, N):
        if S[i] != S[k]:
            L = "RGB"
            L = L.replace(S[i],"",1)
            L = L.replace(S[k],"",1)
            if L == "R":
                ans += R[k]-R[i]
            if L == "G":
                ans += G[k]-G[i]
            if L == "B":
                ans += B[k]-B[i]
            if (k-i) % 2 == 0:
                if S[int((k-i)/2 + i)] == L:
                    ans -= 1


print(ans)

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

Submission status: Time Limit Exceeded

Solution code:

import itertools

N = int(input())
S = input()
ans = 0
i = 0
j = 0
k = 0

Si = ""
Sj = ""
Sk = ""

for v in itertools.combinations(range(N), 3):
    i = v[0]
    j = v[1]
    k = v[2]

    if (j - i) != (k - j):
        Si = S[i]
        Sj = S[j]
        Sk = S[k]
        if Si == Sj or Sj == Sk or Sk == Si:
            continue
        else:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

N = I()
S = S()

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

chars = list(S)

cnt = 0

for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if not check(i, j, k):
                continue
            if chars[i] == chars[j] or chars[j] == chars[k]:
                continue
            if chars[i] != chars[j] and chars[i] != chars[k] and chars[j] != chars[k]:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
b=input()
a=input()

r_list=[]
g_list=[]
b_list=[]
count=0
for k,i in enumerate(a):
  if i=='R' :
    r_list.append(k)
  elif i=='B' :
    b_list.append(k)
  else :
    g_list.append(k)

for i in r_list :
  for j in b_list:
    for k in g_list :
      l=[i,j,k]
      l.sort()
      if l[0]+l[2]!=2*l[1] :
        count+=1
print(count)

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(i+1, N):
        if S[i-1] == S[j-1]:
            pass
        else:
            if (S[i-1] == "R" and S[j-1] == "G" )or (S[i-1] == "G" and S[j-1] == "R"):
                k = "B"
            elif (S[i-1] == "R" and S[j-1] == "B") or (S[i-1] == "B" and S[j-1] == "R"):
                k = "G"
            else:
                k = "R"
            S_cp = S[j:N]
            count += S_cp.count(k)
            if 2*j-i <= N :
                if S[2*j-i-1] == k:
                    count -= 1
print(count)

Submission status: Time Limit Exceeded

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

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

def main():
    n = int(input())
    s = input()
    ans = 0
    for i in range(n):
        ans += sum([sum([1 if bool3(s[i],s[j],s[k]) and bool2(i,j,k) else 0 for k in range(j)]) for j in range(i)])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    c = 0

    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']

    for r in R:
        for g in G:
            for b in B:
                l = sorted([r,g,b])
                if l[2]-l[1] == l[1]-l[0]:
                    continue
                else:
                    c = c+1

    # for i in range(N):
    #     for j in range(i, N):
    #         if S[i]==S[j]:
    #             continue
    #         for k in range(j, N):
    #             if S[j] == S[k] or S[i] == S[k] or j-i == k-j:
    #                 continue
    #             else:
    #                 c = c+1
        
    print(c)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
count=0
R=[]
G=[]
B=[]
for i in range(N):
  if S[i]=="R":
    R.append(i)
  if S[i]=="G":
    G.append(i)
  if S[i]=="B":
    B.append(i)
for r in R:
  for g in G:
    for b in B:
      
      if r<g and g<b:
        if b-g!=g-r:
          count+=1
      elif r<b and b<g:
        if g-b!=b-r:
          count+=1
      elif g<r and r<b:
        if b-r!=r-g:
          count+=1
      elif g<b and b<r:
        if r-b!=b-g:
          count+=1
      elif b<r and r<g:
        if g-r!=r-b:
          count+=1
      elif b<g and g<r:
        if r-g!=g-b:
          count+=1
      
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
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)
c = len(r)*len(g)*len(b)
for ri in r:
  for gi in g:
    for bi in b:
      if 3*(max(ri,gi,bi)+min(ri,gi,bi)) == 2*sum([ri,gi,bi]):
        c -= 1
print(c)

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(i+1, N):
        if S[i-1] == S[j-1]:
            pass
        else:
            for k in range(j+1, N+1):
                if S[i-1] == S[k-1]:
                    pass
                else:
                    if S[j-1] == S[k-1]:
                        pass
                    else:
                        if ((j-i) != (k-j)):
                            count += 1


print(count)

Submission status: Time Limit Exceeded

Solution code:
#import numpy as np

def main():
    ##get int
    first_input = list(map(lambda x:int(x), input().split(" ")))
    #second_input = list(map(lambda x:int(x), input().split(" ")))

    ##get float
    #first_input = list(map(lambda x:float(x), input().split(" ")))
    #second_input = list(map(lambda x:float(x), input().split(" ")))

    ##get string
    #first_input = input().split(" ")
    second_input = input().split(" ")

    N = first_input[0]
    S = second_input[0]

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

    print(count)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
import numpy as np
ans = 0
for i in range(0, N):
    for j in range(i, N):
        if i ==j or S[i] == S[j]:
            continue
        s = ['R', 'G', 'B']
        s.remove(S[i])
        s.remove(S[j])
        k_s = np.where(np.array(list(S[j:])) == s[0])[0] + j
        for k in k_s:
            if j == k or S[j] == S[k]:
                continue
            elif j-i != k-j and S[i] != S[k]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np

def solve(N: int, S: str):
    r = []
    g = []
    b = []
 
    for i, x in enumerate(S):
        if x ==  "R":r.append(i)
        elif x =="G":g.append(i)
        else:b.append(i)

    r = np.array(r)
    g = np.array(g)
    b = np.array(b)
  
    sb = set(b)
    ans = 0
    for r_ in r:
        # rg
        for g_ in g[g>r_]:
            # rgb
            if (g_ + (g_ - r_)) in sb:
                ans -= 1
            # brg
            if (r_ - (g_ - r_)) in sb:
                ans -= 1
            # rbg
            if ((r_ + g_)/2) in sb:
                ans -= 1

        # gr
        for g_ in g[g<r_]:
            # grb
            if (r_ + (r_ - g_)) in sb:
                ans -= 1
            # bgr
            if (g_ - (r_ - g_)) in sb:
                ans -= 1
            # gbr
            if ((r_ + g_)/2) in sb:
                ans -= 1
    ans += len(b) * len(g) * len(r)
    print(ans)
  
    return
 
 
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)
 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

l = []
for i in range(1, N+1):
    l.append(i)

c = itertools.combinations(l, 3)

nl = []
for v in c:
    nl.append(list(v))

n = 0
L = []
for i in nl:
    if ((i[1]-i[0]) != (i[2] -i[1])):
        L.append(i)
    else:
        continue

n = 0
for v in L:
    if (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]):
        n = n+1

print(n)

Submission status: Time Limit Exceeded

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

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

for index, color in enumerate(S):
  if color == "R":
    R.append(index)
  if color == "G":
    G.append(index)
  if color == "B":
    B.append(index)

result = 0
import itertools

# results = sum([ (sorted(x)[1] - sorted(x)[0] != sorted(x)[2] - sorted(x)[1]) for x in itertools.product(R, G, B)])
print(sum([ (sorted(x)[1] - sorted(x)[0] != sorted(x)[2] - sorted(x)[1]) for x in itertools.product(R, G, B)]))

Submission status: Time Limit Exceeded

Solution code:
N= int(input())
S=input()
r=[]
g=[]
b=[]
A=0
B=0
C=0
for i in range(N):
    if S[i]=="R":
        r.append(i)
        A+=1
    elif S[i]=="G":
        g.append(i)
        B+=1
    else:
        b.append(i)
        C+=1
k=0
for i in range(A):
    x = r[i]
    for j in range(B):
        y=g[j]
        if 2*x-y in b:
            k+=1
        if 2*y-x in b:
            k+=1
        if (x+y)/2 in b:
            k+=1

print(A*B*C-k)

Submission status: Time Limit Exceeded

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

s_r = []
s_g = []
s_b = []

srappend = s_r.append
sgappend = s_g.append
sbappend = s_b.append

for i in range(n):
  if s[i] == "R":
    srappend(i)
  elif s[i] == "G":
    sgappend(i)
  else:
    sbappend(i)

ans = 0
    
def conditions(a, b):
  if (a+b) % 2 == 0:
    x = (a+b) / 2
    y = 2*b - a
    z = 2*a - b
  else:
    x = -1
    y = 2*b - a
    z = 2*a - b
  return (x, y, z)
  
for item in s_r:
  for item2 in s_g:
    count = 0
    for i in conditions(item, item2):
      if i in s_b:
        count += 1
    ans += len(s_b) - count

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
count=0
for i in range(n):
  for j in range(n):
    if i>=j:
      continue
    for k in range(n):
      if j>=k:
        continue
      if s[i]!=s[j] and 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:
N = int(input())
S = list(str(input()))
from collections import Counter

def solve(idx, lis, dis):
    if len(lis)==1:
        i = lis[0]
        c = Counter(S[idx:])
        if len(S)>dis:
            if S[dis]==i:
                return c[i]-1
            else:
                return c[i]
        else:
            return c[i]
    elif len(lis)==2:
        ans = 0
        p, q = lis
        s = S[idx:]
        for i, j in enumerate(s):
            if j==p:
                ans += solve(i+idx, (q,), i*2+idx)
            elif j==q:
                ans += solve(i+idx, (p,), i*2+idx)
        return ans
    else:
        ans = 0
        p, q, r = lis
        for i, j in enumerate(S):
            if j==p:
                ans += solve(i, (q, r), i)
            elif j==q:
                ans += solve(i, (p, r), i) 
            elif j==r:
                ans += solve(i, (p, q), i) 
        return ans

print(solve(0, ('R', 'G', 'B'), 0))