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:
# coding: utf-8
# Your code here!
#from sys import stdin
#import math
#from functools import reduce
    
#A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
N = int(input())
S = input()
 
counter = 0
 
for k in range(N):
    sk = S[k]
    for j in range(1,k):
        s = 'RGB'
        sj = S[j]
        if (sk == sj):continue
        s = s.replace(sk, '')
        s = s.replace(sj, '')
        diff = k-j
        if j-diff < 0:
            buf = S[0:j]
        #print(j-diff+1,j+1,k+1)
        #print(S)
        #print(S[0:j-diff], S[j-diff+1:j])
        else:
            buf = S[0:j-diff] + S[j-diff+1:j]
        counter = counter + buf.count(s)
        #print(s, buf.count(s), counter)
 
print(counter)

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)
cnt = 0
l = []
for i in range(len(r)):
    for j in range(len(g)):
        if (r[i] + g[j]) % 2 == 0:
            l.append((r[i] + g[j])//2)
        l.append(2 * r[i] - g[j])
        l.append(2 * g[j] - r[i])
for i in range(len(l)):
    if l[i] in b:
        cnt += 1
print(ans-cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
all = ord("R") + ord("G") + ord("B")
for i in range(0, n - 2):
    for j in range(i + 1, n - 1):
        if not s[j] == s[i]:
            moji = chr(all - ord(s[j]) - ord(s[i]))
            temp = s[j + 1:]
            ans += temp.count(moji)
            if j + j - i >= len(s):
                continue
            if s[j + j - i] == moji:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
count=0
R=0
G=0
B=0
for i in range(N):
  if S[i]=="R":
    R+=1
  if S[i]=="G":
    G+=1
  if S[i]=="B":
    B+=1
count=0
for i in range(N-2):
  for j in range(i,N-1):
    k=j+j-i
    try:
      if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
        count+=1
    except:
      pass
    
print(R*G*B-count)

Submission status: Time Limit Exceeded

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

    count_R = [0]
    count_G = [0]
    count_B = [0]
    d = {'R': count_R, 'G': count_G, 'B': count_B}
    for c in s:
        count_R.append(count_R[-1]+1) if c == 'R' else count_R.append(count_R[-1])
        count_G.append(count_G[-1]+1) if c == 'G' else count_G.append(count_G[-1])
        count_B.append(count_B[-1]+1) if c == 'B' else count_B.append(count_B[-1])

    res = 0
    for i in range(n-2):
        for k in range(i+1, n-1):
            if s[i] == s[k]:
                continue
            st = {'R', 'G', 'B'}
            st.discard(s[i])
            st.discard(s[k])
            c = st.pop()
            cnt = d[c]
            res += cnt[-1] - cnt[k]
            if k+k-i < n and s[k+k-i] == c:
                res -= 1

    print(res)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
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)

def count(X,Y,Z):
  count = 0
  for r in X:
    for g in Y:
      if (r+g)%2==0 and (r+g)//2 in Z:
        count += 1
      if 2*r-g in Z:
        count += 1
      if 2*g-r in Z:
        count += 1
  return count

if len(R)>=max(len(G),len(B)):
  print(len(R)*len(G)*len(B)-count(G,B,R))
elif len(G)>=len(R):
  print(len(R)*len(G)*len(B)-count(R,B,G))
else:
  print(len(R)*len(G)*len(B)-count(R,G,B))

Submission status: Time Limit Exceeded

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

count = 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 i -j == j - k:
                continue
            if S[i] != S[j] and S[k] != S[j] and S[i] != S[k]:
                count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
an = input()
alist = list(an)
count=0
for i in range(0,n-2):
  a = alist[i]
  for j in range(i,n-1):
    b = alist[j]
    for k in range(j,n):
      c = alist[k]
      if a!=b and b!=c and c!=a and k-j!=j-i:
        count+=1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
count = 0
N = int(input())
slist = deque(list(input()))
maslist = ["R", "G", "B"]
lists = maslist.copy()
for a in range(N-2):
    lists.remove(slist[a])
    for b in range(a+1, N-1):
        if slist[b] in lists:
            lists.remove(slist[b])
            t=list(slist)[b+1:N].count(lists[0])
            count+=t
            lists.append(slist[b])
    lists = maslist.copy()
   
for a in range(N-2):
    for b in range(a+1,N):
        if 2*b-a<N and slist[a]!=slist[b] and slist[b]!=slist[2*b-a] and slist[a]!=slist[2*b-a]:
                count -= 1
    
print(count)

Submission status: Time Limit Exceeded

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

import sys


def input():
    return sys.stdin.readline()


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

    all_count = 0
    count = 0
    for i in range(n):
        for j in range(i+1, n):
            if s[i] == s[j]:
                continue

            if s[i] == "R" and s[j] == "G":
                target_str = "B"
            elif s[i] == "R" and s[j] == "B":
                target_str = "G"
            elif s[i] == "G" and s[j] == "R":
                target_str = "B"
            elif s[i] == "G" and s[j] == "B":
                target_str = "R"
            elif s[i] == "B" and s[j] == "R":
                target_str = "G"
            elif s[i] == "B" and s[j] == "G":
                target_str = "R"

            count += s.count(target_str, j+1, n)

            if 2*j-i < n:
                if target_str == s[2*j-i]:
                    count -= 1
            """
            for k in range(j+1, n):
                if j - i == k -j:
                    continue
                
                if s[i] == s[k] or s[j] == s[k]:
                    continue

                count += 1
            """

    print(count)


if __name__ == "__main__":
    resolve()

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)
cnt = 0
for i in range(len(r)):
    for j in range(len(g)):
        if (r[i] + g[j]) % 2 == 0 and (r[i] + g[j]) // 2 in b:
            cnt += 1
        if 2 * r[i] - g[j] in b:
            cnt += 1
        if 2 * g[j] - r[i] in b:
            cnt += 1
print(ans-cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

for i, s in enumerate(S):
  if s == 'R':
    r.append(i+1)
  elif s == 'G':
    g.append(i+1)
  elif s == 'B':
    b.append(i+1)

count = 0
for i in r:
  for j in g:
    for k in b:
      if abs(j-i) != abs(k-j) and abs(k-j) != abs(k-i) and abs(k-i) != abs(j-i):
        count += 1
      else:
        pass
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(input())
count = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    if l[i] != l[j]:
      for k in range(j+1,n):
        if l[j] != l[k] and l[i] != l[k] 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

c = Counter(S)
ans = c['B']*c['G']*c['R']
for i in range(N-1):
    for j in range(i, N):
        if S[i]==S[j]:
            continue
        elif i+(j-i)*2>N-1:
            continue
        s = S[i+(j-i)*2]
        if s!=S[i] and s!=S[j]:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
#listG = [j for j, x in enumerate(S) if x == 'G']
#listB = [k for k, x in enumerate(S) if x == 'B']
SUMSUM = 0
for a in range(N-2):
    #Rから
    if S[a] == "R":
        for b in range (N -2 - a):
            k = 0
            if S[a+b+1] == "G":
                k = S[(a+b+2):].count("B")
                if a+b+2+b <= (N-1):
                    if S[a+b+b+2] == "B":
                        SUMSUM -= 1
                SUMSUM += k
            elif S[a+b+1] == "B":
                k = S[(a+b+2):].count("G")
                if a+b+1+b+1 <= (N-1):
                    if S[a+b+1+b+1] == "G":
                        SUMSUM -= 1
                SUMSUM += k
    elif S[a] == "G":
        k = 0
        for b in range (N -2 - a):
            if S[a+b+1] == "B":
                k = S[(a+b+2):].count("R")
                if a+b+b+2 <= (N-1):
                    if S[a+b+1+b+1] == "R":
                        SUMSUM -= 1
                SUMSUM += k
            elif S[a+b+1] == "R":
                k = S[(a+b+2):].count("B")
                if a+b+1+b+1 <= (N-1):
                    if S[a+b+1+b+1] == "B":
                        SUMSUM -= 1
                SUMSUM += k
    elif S[a] == "B":
        k = 0
        for b in range (N -2 - a):
            if S[a+b+1] == "G":
                k = S[a+b+2:].count("R")
                if a+b+1+b+1 <= (N-1):
                    if S[a+b+1+b+1] == "R":
                        SUMSUM -= 1
                SUMSUM += k
            elif S[a+b+1] == "R":
                k = S[a+b+2:].count("G")
                if a+b+1+b+1 <= (N-1):
                    if S[a+b+2+b] == "G":
                        SUMSUM -= 1
                SUMSUM += k
print(SUMSUM)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

from collections import defaultdict
from collections import deque
def load(vtype=int):
    return vtype(input().strip())
def load_list(seplator=" ", vtype=int):
    return [vtype(v) for v in input().strip().split(seplator)]
def exit():
    import sys
    sys.exit(0)
def perm_sub(li, used):
    if len(li) == len(used):
        return [deque()]
    k = []
    for i in range(len(li)):
        if i in used:
            continue
        used.add(i)
        sub_list = perm_sub(li, used)
        for sub in sub_list:
            sub.appendleft(li[i])
        k.extend(sub_list)
        used.discard(i)
    return k
def perm_li(li):
    return perm_sub(li, set())
def perm_n(n):
    return perm_sub(list(range(n)), set())
def join_i(li, sep=""):
    return sep.join([str(e) for e in li])
def li2n(li):
    n, base = 0, 1
    for i in range(len(li)-1, -1, -1):
        n += li[i] * base
        base *= 10
    return n
def sli2ili(li):
    return [int(s) for s in li] 
def prime_list(n):
    li = [i for i in range(2, n+1)]
    for i in range(len(li)):
        if li[i] >= int(math.sqrt(n)):
            break
        if li[i] == -1:
            continue
        for j in range(i+1, len(li)):
            if li[j] % li[i] == 0:
                li[j] = -1
    return [n for n in li if n != -1]
def gcd(a, b):
    a, b = max(a, b), min(a, b)
    while True:
        r = a % b
        if r == 0: return b
        a, b = b, r
def lcm(a, b):
    return int(a * b / gcd(a,b))
def all_subset(li):
    s = []
    n = len(li)
    for bit in range(0, (1<<(n+1))):
        ss = set()
        for i in range(0, n):
            if (bit & (1 << i)):
                ss.add(li[i])
        s.append(ss)
    return s
def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)
def mCn(m, n):
    def factorial_ntimes(m, n):
        r = 1
        while n:
            r *= m
            m -= 1
            n -= 1
        return r
    return int(factorial_ntimes(m, n) / factorial(n))


def notab(a, b):
    s = set((a,b))
    for c in "RGB":
        if c not in s:
            return c

n  = load()
s = list(load(str))

mp = defaultdict(set)
for i in range(n):
    mp[s[i]].add(i)

cnt = 0
for i in range(0, n):
    first = s[i]
    mp[first].remove(i)
    for j in range(i + 1, n):
        if s[j] == first:
            continue
        second = s[j]
        target = notab(first, second)
        for k in range(j+1, n):
            if k-j == j-i or s[k] != target:
                continue 
            # print(i, j, k)
            cnt +=1
        # if j + (j - i) < n:
        #     tmp = s[j+ (j-i)]
        #     cnt += s[j+1:].count(target)
        #     s[j+ (j-i)] = tmp
        # else:
        #     cnt += s[j+1:].count(target) 
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N= int(input())
S= input()
correct = ["RGB","RBG","GBR","GRB","BRG","BGR"]

cnt = 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%s%s"%(S[i],S[j],S[k]) in correct:
					cnt+=1

print(cnt)

Submission status: Time Limit Exceeded

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

num_r, num_g = S.count('R'), S.count('G')
num_b = N - num_r - num_g
max_ans = num_r * num_g * num_b
count_not_ans = 0
S = list(S)

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

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

Submission status: Time Limit Exceeded

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

dict_x = {"R":1, "G":2, "B":3}
s = [dict_x[s] for s in s]

result = 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 (j - i) != (k - j):
                if s[i] * s[j] * s[k] == 6:
                    result += 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
Rindexes = [i for i, x in enumerate(S) if x == 'R']
Gindexes = [i for i, x in enumerate(S) if x == 'G']
Bindexes = [i for i, x in enumerate(S) if x == 'B']

total = len(Rindexes) * len(Gindexes) * len(Bindexes)
count = 0
for i in range(N-2):
  for j in range(i+1, N-1):
    k = 2*j - i
    if S[i] != S[j]:
      if S[i] != 'R' and S[j] != 'R' and k in Rindexes:
        count += 1
      elif S[i] != 'G' and S[j] != 'G' and k in Gindexes:
        count += 1
      elif S[i] != 'B' and S[j] != 'B' and k in Bindexes:
        count += 1
print(total - count)

Submission status: Time Limit Exceeded

Solution code:
str_length = int(input())
rgb_str = str(input())

flag = 0
for num_1 in range(1, str_length + 1):
	for num_2 in range(num_1, str_length + 1):
		for num_3 in range(num_2, str_length + 1):
			if rgb_str[num_1 - 1] != rgb_str[num_2 - 1] and rgb_str[num_2 - 1] != rgb_str[num_3 - 1] and rgb_str[num_1 - 1] != rgb_str[num_3 - 1]:
				if num_2 - num_1 != num_3 - num_2:
					flag += 1
				else:
					continue
			else:
				continue
print(str(flag))

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        if RGB[i]==RGB[j]:
            continue
        for k in range(j+1,n):
            if RGB[i]!=RGB[k] and RGB[j]!=RGB[k] and j-i != k-j:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

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

for i, s in enumerate(S):
  if s == 'R':
    r.append(i+1)
    g.append(0)
    b.append(0)
  elif s == 'G':
    r.append(0)
    g.append(i+1)
    b.append(0)
  elif s == 'B':
    r.append(0)
    g.append(0)
    b.append(i+1)

count = 0
for i in r:
  for j in g:
    for k in b:
      if i != 0 and j != 0 and k != 0 and abs(j-i) != abs(k-j) and abs(k-j) != abs(k-i) and abs(k-i) != abs(j-i):
        count += 1
      else:
        pass
print(count)

Submission status: Time Limit Exceeded

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

diff_dic = {}
diff_dic[('R', 'G')] = []
diff_dic[('R', 'B')] = []
diff_dic[('G', 'R')] = []
diff_dic[('G', 'B')] = []
diff_dic[('B', 'G')] = []
diff_dic[('B', 'R')] = []

R_lst = [0] * N
G_lst = [0] * N
B_lst = [0] * N

r_cnt = 0
g_cnt = 0
b_cnt = 0

cnt = 0
for i in range(N):
    if S[i] == 'R':
        r_cnt += 1
    elif S[i] == 'G':
        g_cnt += 1
    else:
        b_cnt += 1

    R_lst[i] = r_cnt
    G_lst[i] = g_cnt
    B_lst[i] = b_cnt

    for j in range(i):
        d = i - j
        if (S[i] == 'R' and S[j] == 'G') or (S[i] == 'G' and S[j] == 'R'):
            if j - 1 >=0 and B_lst[j - 1] > 0:
                cnt += B_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'B':
                    cnt -= 1

        if (S[i] == 'G' and S[j] == 'B') or (S[i] == 'B' and S[j] == 'G'):
            if j - 1 >=0 and R_lst[j - 1] > 0:
                cnt += R_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'R':
                    cnt -= 1

        if (S[i] == 'B' and S[j] == 'R') or (S[i] == 'R' and S[j] == 'B'):
            if j - 1 >= 0 and G_lst[j - 1] > 0:
                cnt += G_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'G':
                    cnt -= 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
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)

def count(X,Y,Z):
  count = 0
  for r in X:
    for g in Y:
      len_B = len(Z)
      if (r+g)%2==0 and (r+g)//2 in Z:
        len_B -= 1
      if 2*r-g in Z:
        len_B -= 1
      if 2*g-r in Z:
        len_B -= 1
      count += len_B
  return count

if len(R)>=max(len(G),len(B)):
  print(count(G,B,R))
elif len(G)>=len(R):
  print(count(R,B,G))
else:
  print(count(R,G,B))

Submission status: Time Limit Exceeded

Solution code:
K = int(input())
S = list(input())
total = 0

for a in range(0,K-2):
  for b in range(a+1,K-1):
    if S[a] != S[b]:
      for c in range(b+1,K):
        if S[c] != S[b] and S[c] != S[a] and b-a != c-b:
          total += 1
print(total)

Submission status: Time Limit Exceeded

Solution code:
count = 0
N = int(input())
slist = list(input())
maslist = ["R", "G", "B"]
lists = maslist.copy()
for a in range(N-2):
    lists.remove(slist[a])
    for b in range(a+1, N-1):
        if slist[b] in lists:
            lists.remove(slist[b])
            t=slist[b+1:N].count(lists[0])
            count+=t
            lists.append(slist[b])
    lists = maslist.copy()
   
for a in range(N-2):
    for b in range(a+1,N):
        if 2*b-a<N and slist[a]!=slist[b] and slist[b]!=slist[2*b-a] and slist[a]!=slist[2*b-a]:
                count -= 1
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
r = [i for i, x in enumerate(s) if x == 'R']
g = [i for i, x in enumerate(s) if x == 'G']
b = [i for i, x in enumerate(s) if x == 'B']
ans = len(r)*len(g)*len(b)
num = 0
for i in r:
    for j in g:
        if i in b:
            print(i)
        a = [(i+j)/2, 2*i-j, 2*j-i]
        c = list(set(a).intersection(b))
        num += len(c)
        
print(ans-num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = list()
for i in range(n):
    if s[i] == "R":
        r.append(i)
b = list()
for i in range(n):
    if s[i] == "B":
        b.append(i)
g = list()
for i in range(n):
    if s[i] == "G":
        g.append(i)
a = len(r) * len(b) * len(g)
for i in r:
    for j in b:
        k = abs(i - j)
        if i + k in g:
            a -= 1
        if i - k in g:
            a -= 1
        if j + k in g:
            a -= 1
        if j - k in g:
            a -= 1
        if (i + j) / 2 in g:
            a -= 1
print(a)

Submission status: Time Limit Exceeded

Solution code:
 
n=int(input())
s=input()
#n=39
#s="RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"

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)

lenr=len(r)
leng=len(g)
lenb=len(b)

isum=0
for ir in range(len(r)):
    for ig in range(len(g)):
        imin=max(r[ir],g[ig])
        if abs(r[ir]-g[ig])+imin in b: 
            isum=isum+1
for ir in range(len(r)):
    for ib in range(len(b)):
        imin=max(r[ir],b[ib])
        if abs(r[ir]-b[ib])+imin in g: 
            isum=isum+1
for ig in range(len(g)):
    for ib in range(len(b)):
        imin=max(g[ig],b[ib])
        if abs(g[ig]-b[ib])+imin in r: 
            isum=isum+1

isum=lenr*leng*lenb-isum
                
print(isum)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

N = int(input())
s = input()
cnt = Counter(s)
R = cnt.get('R', 0)
G = cnt.get('G', 0)
B = cnt.get('B', 0)
ans = R * G * B
for i in range(N-1):
  for j in range(i+1, N):
    if s[i] != s[j] and 2*j-i < N and s[i] != s[2*j-i] and s[j] != s[2*j-i]:
      ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

num_r, num_g, num_b = S.count('R'), S.count('G'), S.count('B')
max_ans = num_r * num_g * num_b
count_not_ans = 0
S = list(S)

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

print(max_ans - count_not_ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
import sys
from collections import Counter
from itertools import permutations, combinations
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10 ** 6)

def input():
    return sys.stdin.readline()[:-1]

def main():
    n = int(input())
    s = input()
    ans = 0
    for idx1, letter1 in enumerate(s):
        for idx2, letter2 in enumerate(s[idx1+1:]):
            if letter1 != letter2:
                for idx3, letter3 in enumerate(s[idx2+idx1+2:]):
                    if letter1 != letter3 and letter2 != letter3 and idx2 != idx3:
                        ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n=n0()
s=s0()
r=[]
g=[]
b=[]
c=0
for x in s:
    if x=="R":
        r.append(c)
    elif x=="G":
        g.append(c)
    else:
        b.append(c)
    c+=1
ans=0
for r1 in r:
    for g1 in g:
        for b1 in b:
            k=max(r1,g1,b1)
            i=min(r1,g1,b1)
            j=[x for x in [r1,g1,b1] if x!=k and x!= i][0]
            if j-i!=k-j:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

counta = 0
i = 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 itertools.product(R, G, B):
    j = sorted(list(i))
    if j[2] - j[1] != j[1] - j[0]:
        counta += 1
print(counta)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s_list = list(s)
ans = 0
total = s_list.count("R") * s_list.count("G") * s_list.count("B")
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_list[i] != s_list[j]) and (s_list[j] != s_list[k]) and (s_list[k] != s_list[i]):
                    ans +=1
print(total - ans)

Submission status: Time Limit Exceeded

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

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

	ans = 0
	r = []
	g = []
	b = []
	for i in range(3):
		if s[i] == 'R':
			r.append(i)
		elif s[i] == 'G':
			g.append(i)
		else:
			b.append(i)

	for i in range(3,n):
		if s[i] == 'R':
			r.append(i)
		elif s[i] == 'G':
			g.append(i)
		else:
			b.append(i)

		for j in range(i-2):
			if (j-i) % 2 == 0:
				if s[i] == s[j]:
					continue
				elif 'R' not in [s[i],s[j]]:
					ans += bisect.bisect_left(r,i) - bisect.bisect_left(r,j)
					index = bisect.bisect_left(r,j + (i-j)//2 )
					if index < len(r):
						if r[index] == j + (i-j)//2:
							ans -= 1
				elif 'G' not in [s[i],s[j]]:
					ans += bisect.bisect_left(g,i) - bisect.bisect_left(g,j)
					index = bisect.bisect_left(g,j + (i-j)//2 )
					if index < len(g):
						if g[index] == j + (i-j)//2:
							ans -= 1
				elif 'B' not in [s[i],s[j]]:
					ans += bisect.bisect_left(b,i) - bisect.bisect_left(b,j)
					index = bisect.bisect_left(b,j + (i-j)//2 )
					if index < len(b):
						if b[index] == j + (i-j)//2:
							ans -= 1
			else:
				if s[i] == s[j]:
					continue
				elif 'R' not in [s[i],s[j]]:
					ans += bisect.bisect_left(r,i) - bisect.bisect_left(r,j)

				elif 'G' not in [s[i],s[j]]:
					ans += bisect.bisect_left(g,i) - bisect.bisect_left(g,j)

				elif 'B' not in [s[i],s[j]]:
					ans += bisect.bisect_left(b,i) - bisect.bisect_left(b,j)


	print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
SL = list(S)
cnt = 0
nr = SL.count('R')
ng = SL.count('G')
nb = SL.count('B')
all = nr * ng * nb
cnt = 0
for i in range(len(S)):
    for j in range(i+1,len(S)):
        for k in range(j+1,len(S)):
            if (not (S[i] == S[j] or S[i] == S[k] or S[j] == S[k])) and (j-i) == (k-j):
                cnt += 1
print(all-cnt)

Submission status: Time Limit Exceeded

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

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):
    t = s[j:n]
    if s[i] != s[j]:
      cnt += len(t) - t.count(s[i]) - t.count(s[j])
      if n > 2*j-i:
        if s[2*j-i] != s[i] and s[2*j-i] != s[j]:
          cnt -= 1

      
print(cnt)

Submission status: Time Limit Exceeded

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

dict_x = {"R":1, "G":2, "B":3}
s = [dict_x[s] for s in s]

result = 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] * s[k] == 6:
                    result += 1

print(result)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

lis = [[a,b,c] for a in S["R"] for b in S["G"] for c in S["B"]]
ans = 0
for i,j,k in lis:
	if i > k:
		tmp = k
		k = i
		i = tmp
	if j > k:
		tmp = k
		k = j
		j = tmp
	if i > j:
		tmp = j
		j = i
		i = tmp	
	if j-i!=k-j:
		ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

new_list = []

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)
    else:
        b_list.append(i)

num = 0

for r in r_list:
    for g in g_list:
        for b in b_list:
            list_rgb = [r, g, b]
            list_rgb.sort()
            if (list_rgb[2] - list_rgb[1]) == (list_rgb[1] - list_rgb[0]):
                pass
            else:
                num += 1

print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
r = g =  b = 0
for s in S:
    if s == 'R':
        r += 1
    elif s == 'G':
        g += 1
    else:
        b += 1

double = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        k = (j - i) * 2 + i
        if k >= N:
            continue
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            double += 1
print(r * g * b - double)