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()
r = []
g = []
b = []
for i in range(n):
  if s[i] == 'R':
    r.append(i)
  elif s[i] == 'G':
    g.append(i)
  else:
    b.append(i)
ans = 0
for ri in r:
  for gi in g:
    for bi in b:
      a = [ri,gi,bi]
      a.sort()
      if a[1]-a[0] != a[2]-a[1]:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

ans = 0
num_list = [i for i in range(N)]
for v in itertools.combinations(num_list, 3):
    left_num = v[0]
    mid_num = v[1]
    right_num = v[2]
    if abs(left_num - mid_num) != abs(mid_num - right_num):
        if S[left_num] != S[mid_num] and S[mid_num] != S[right_num] and S[left_num] != S[right_num]:
            ans += 1
            
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
b=input()
counter=0
for i in range(a):
    for j in range(i+1,a):
        for k in range(j+1,a):
            if b[i]!=b[j] and b[j]!=b[k] and b[k]!=b[i]:
                if j-i!=k-j:
                    counter+=1
print(counter)

Submission status: Time Limit Exceeded

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

rli = []
gli = []
bli = []
for i in range(N):
    if S[i] == "R":
        rli.append(i+1)
    if S[i] == "G":
        gli.append(i+1)
    if S[i] == "B":
        bli.append(i+1)

ans = 0
for i in rli:
    for j in gli:
        sa = max(i,j)-min(i,j)
        scr = len(bli)
        if min(i,j)-sa in bli:
            scr -= 1
        if (i+j)/2 in bli:
            scr -= 1
        if max(i,j)+sa in bli:
            scr -= 1
        ans += scr

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
seq = input().strip()
ans = 0
r = []
g = []
b = []
for i in range(len(seq)):
    if seq[i] == 'R':
        r.append(i)
    elif seq[i] == 'G':
        g.append(i)
    else:
        b.append(i)

for i, val in enumerate(seq[:-2]):
    for j, val in enumerate(seq[i+1:-1], i+1):
        if seq[i] == seq[j]:
            continue
        if seq[i] in ('R', 'G') and seq[j] in ('R', 'G'):
            arr = b
        elif seq[i] in ('R', 'B') and seq[j] in ('R', 'B'):
            arr = g
        else:
            arr = r

        for k in arr:
            if k > j and j-i != k -j:
                ans += 1
        # for k, val in enumerate(seq[j+1:], j+1):
        #     if seq[i] != seq[k] and seq[j] != seq[k] and j-i != k-j:
        #         ans +=1
print(ans)

Submission status: Time Limit Exceeded

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

rl = []
gl = []
bl = []
rld = [0]*(n)
gld = [0]*(n)
bld = [0]*(n)

for i in range(len(s)):
  if s[i] == 'R':
    rl += [i]
    rld[i] = 1 
  elif s[i] == 'G':
    gl += [i]
    gld[i] = 1 
  else:
    bl += [i]
    bld[i] = 1

ans = 0 
for i in rl:
  for j in gl:
    #print(i, j, j+j-i, bl[j+1:])
    #print(bl[j+1:])
    if i >= j:
      continue
    ans += sum(bld[j+1:])
    if j+j-i < n:
      ans -= bld[j+j-i]

for i in rl:
  for j in bl:
    if i >= j:
      continue
    ans += sum(gld[j+1:])
    if j+j-i < n:
      ans -= gld[j+j-i]

for i in gl:
  for j in bl:
    if i >= j:
      continue
    ans += sum(rld[j+1:])
    if j+j-i < n:
      ans -= rld[j+j-i]

for i in gl:
  for j in rl:
    if i >= j:
      continue
    ans += sum(bld[j+1:])
    if j+j-i < n:
      ans -= bld[j+j-i]
for i in bl:
  for j in gl:
    if i >= j:
      continue
    ans += sum(rld[j+1:])
    if j+j-i < n:
      ans -= rld[j+j-i]

for i in bl:
  for j in rl:
    if i >= j:
      continue
    ans += sum(gld[j+1:])
    if j+j-i < n:
      ans -= gld[j+j-i]
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!

# coding: utf-8
# Your code here!
# coding: utf-8
import sys
sys.setrecursionlimit(10000000)

#const
dxdy=((1,0),(0,1))
# my functions here!
def pin(type=int):
    return map(type,input().rstrip().split())

def resolve():  
    N=int(input())
    S=input()
    r,g,b=[],[],[]
    for i,s in enumerate(S):
        if s=="R":r.append(i)
        elif s=="B":b.append(i)
        else: g.append(i)
    #print(r,g,b)
    #RGBのリストが作れたので、このうち１個ずつとっていけば
    #<1>の条件を満たすことができる（ijkの順序が決まっていることから、
    #最後にこれらが<2>を満たすことを確認する？または、満たさないものを除く
    #<2> i<=j<=k　に対して　j-i = k-j でない
    #"この式を整理すると"k-j*2+i=0ではない:i+j+k=j*3ではない
    #逆にこの式を満たす（平均の関係になっているなら、jは必ず真ん中になる
    
    cnt=0
    for R in r:
        for G in g:
            for B in b:
                temp= R+G+B
                if temp==R*3 or temp==G*3 or temp == B*3:
                    cnt+=1
    print(len(r)*len(g)*len(b)-cnt)
        
"""         
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じ,るの忘れてるだろ
"""
import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4
RRGB"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"""
        output = """1800"""
        self.assertIO(input, output)


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline  # 改行コード「\n」を呼んでしまうため、文字数など、注意！！

n = int(input().replace("\n",""))
s = input().replace("\n","")

ans = 0

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

for i in range(N):
    first_s = S[i]
    for j in range(i+1, N):
        if S[j] != first_s:
            second_s = S[j]
            for k in range(j+1, N):
                if (S[k] != first_s) and (S[k] != second_s):
                    if k == 2*j-i:
                        continue
                    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools as it

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

s = [i for i in s]
r_indeces = [i for i in range(n) if s[i] == 'R']
g_indeces = [i for i in range(n) if s[i] == 'G']
b_indeces = [i for i in range(n) if s[i] == 'B']

indeces = [sorted(list(i))
           for i in it.product(r_indeces, g_indeces, b_indeces)]
indeces = [i for i in indeces if i[1] - i[0] != i[2] - i[1]]
print(len(indeces))

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

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

import sys
import math

sys.setrecursionlimit(10 ** 6)

input_line = input()
inputs = input_line.split(' ')
N, = [int(s) for s in inputs]

input_line = input()
s = list(input_line)
sl = len(s)

two = {}
for i in range(sl):
    for j in range(i+1,sl):
        if s[i]!=s[j]:
            if s[i]+s[j] in two:
                two[s[i]+s[j]].append((i,j))
            else:
                two[s[i]+s[j]] = [(i, j)]


ans = 0
for k1, v1 in two.items():
    for k2, v2 in two.items():
        if k1[1]==k2[0] and k1[0]!=k2[1]:
            for t1 in v1:
                for t2 in v2:
                    if t1[1]==t2[0] and t1[1]-t1[0]!=t2[1]-t2[0]:
                        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=input()
c=0
for i in range(N-2):
 B2=A[i+1]
 for j in range(i+1):
  B1=A[j]
  if B1!=B2:
   for k in range(N-i-2):
    B3=A[i+2+k]
    if B2==B3 or B1==B3 or i==j+k:
     continue
    else:
     c+=1
print(c)

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

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(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]:
                count+=1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if n == 1 or n == 2:
    print(0)
    
else:
    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]:
                    if s[i] != s[k]:
                        if s[j] != s[k]:
                            if j-i != k-j:
                                x += 1

    print(x)

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

Submission status: Time Limit Exceeded

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

r = [i for i, x in enumerate(s_list) if x == 'R']
g = [i for i, x in enumerate(s_list) if x == 'G']
b = [i for i, x in enumerate(s_list) if x == 'B']

ans = 0
for num_r in r:
    for num_g in g:
        for num_b in b:
            num_list = [num_r, num_g, num_b]
            num_list.sort()
            if num_list[2]-num_list[1] != num_list[1]-num_list[0]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3.6

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

Submission status: Time Limit Exceeded

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

sum = 0

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

Submission status: Time Limit Exceeded

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

M = [[0] * 3 for _ in range(N+1)]

for i in range(N-1, -1, -1):
    for j in range(3):
        M[i][j] = M[i+1][j]
    if S[i] == "R":
        M[i][0] += 1
    elif S[i] == "G":
        M[i][1] += 1
    else:
        M[i][2] += 1

ans = 0

T = "RGB"


for i in range(N-2):
    for j in range(i+1, N-1):
        m = [0, 0, 0]
        if S[i] == "R":
            m[0] += 1
        elif S[i] == "G":
            m[1] += 1
        else:
            m[2] += 1

        k = j + (j-i)
        if S[j] == "R":
            if m[0] == 0:
                m[0] += 1
                idx = m.index(0)
                ans += M[j+1][idx]
                if k < N and S[k] == T[idx]:
                    ans -= 1
            else:
                continue
        elif S[j] == "G":
            if m[1] == 0:
                m[1] += 1
                idx = m.index(0)
                ans += M[j+1][idx]
                if k < N and S[k] == T[idx]:
                    ans -= 1
            else:
                continue
        else:
            if m[2] == 0:
                m[2] += 1
                idx = m.index(0)
                ans += M[j+1][idx]
                if k < N and S[k] == T[idx]:
                    ans -= 1
            else:
                continue

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if j-i == k-j:
                continue
            elif len(set([s[i],s[j],s[k]])) >= 3:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

import sys
import math

debug = False

def log(text):
    if debug:
        print(text)


def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        # for line in sys.stdin:
        #     lines.append(line)
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    
    n = int(lines[0])
    s = lines[1]

    return (n, s)


def solve(n, s):

    result = 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[j] != s[k] and j - i != k - j:
                    result = result + 1
    
    return result

def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

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

Submission status: Time Limit Exceeded

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

count = {"R": 0, "G": 0, "B": 0}
char2num = {"R": 1, "G": 10, "B": 100}
count_list = []

for char in S[::-1]:
    count[char] += 1
    count_list.append((count["R"], count["G"], count["B"]))
count_list = count_list[::-1]
        

ans = 0
for i in range(N-2):
    one = char2num[S[i]]
    for j in range(i+1, N-1):
        two = char2num[S[j]]
        if one != two:
            tmp = j+(j-i) < N
            if one+two == 11:
                if tmp and S[j+(j-i)] == "B":
                    ans += count_list[j][2] - 1
                else:
                    ans += count_list[j][2]
                    
            if one+two == 101:
                if tmp and S[j+(j-i)] == "G":
                    ans += count_list[j][1] - 1
                else:
                    ans += count_list[j][1]
                    
            if one+two == 110:
                if tmp and S[j+(j-i)] == "R":
                    ans += count_list[j][0] - 1
                else:
                    ans += count_list[j][0]
print(ans)

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')] = []

for i in range(N):
    for j in range(i + 1, N):

        if S[i] == 'R':
            if S[j] != 'R':
                diff_dic[('R', S[j])].append((i, j - i))

        if S[i] == 'G':
            if S[j] != 'G':
                diff_dic[('G', S[j])].append((i, j - i))

        if S[i] == 'B':
            if S[j] != 'B':
                diff_dic[('B', S[j])].append((i, j - i))

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

        for j, d in diff_dic[('B', 'G')]:
            if j > i and (j - i) != d:
                cnt += 1

    if S[i] == 'G':
        for j, d in diff_dic[('R', 'B')]:
            if j > i and (j - i) != d:
                cnt += 1

        for j, d in diff_dic[('B', 'R')]:
            if j > i and (j - i) != d:
                cnt += 1

    if S[i] == 'B':
        for j, d in diff_dic[('R', 'G')]:
            if j > i and (j - i) != d:
                cnt += 1

        for j, d in diff_dic[('G', 'R')]:
            if j > i and (j - i) != d:
                cnt += 1

print(cnt)

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

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

print(count)

Submission status: Time Limit Exceeded

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

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

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

print(ans)

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+1, N):
        if flag_check(j, S):
            flag_make(j, S)
        else:
            continue
        for k in range(j+1, N):
            if flag_check(k, S):
                if rule2(i, j, k):
                    result += 1
            else:
                continue
        flag_clear(j, S)
    flag_clear(i, S)

print(result)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    res = 0
    r = []
    g = []
    b = []
    for i, c in enumerate(s):
        if c == 'R':
            r.append(i)
        elif c == 'G':
            g.append(i)
        else:
            b.append(i)
    
    cnt = 0
    for i in r:
        for j in g:
            for k in b:
                if abs(j-i) != abs(k-j) and abs(j-i) != abs(k-i) and abs(k-j) != abs(k-i):
                    cnt += 1
    print(cnt)
                    


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        for k in range(j+1, N):
            if set([S[i], S[j], S[k]]) == {"R", "G", "B"} and j-i != k-j:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(N):
  for j in range(i + 1, N - 1):
    if S[i] != S[j]:
      for k in range(j + 1, N):
        if 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())
STR = input()

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import copy
import math
import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
from decimal import *

import math
from functools import reduce

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

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

if __name__ == '__main__':
    n = int(input())

    a = input()
    listr = [0]*n
    listg = [0]*n
    listb = [0]*n
    r =0
    g =0
    b =0

    count = 0

    for i in range(n):

        if a[i] == "R":
            r +=1
        if a[i] == "G":
            g +=1
        if a[i] == "B":
            b +=1
        listr[i] =r
        listb[i] =b
        listg[i] =g

    for i in range(n):

        if a[i] == "R":
            r -=1
        if a[i] == "G":
            g -=1
        if a[i] == "B":
            b -=1
        listr[i] =r
        listb[i] =b
        listg[i] =g

    for i in range(n-2):
        for j in range(i+1,n-1):
            if a[i] != a[j]:
                if a[i] =="R" and a[j] =="G":
                    count += listb[j]
                    if (j-i)+j < len(a) and a[(j-i)+j] =="B" and listb[j] >0:
                        count -=1
                    continue
                if a[i] =="R" and a[j] =="B":
                    count += listg[j]
                    if (j-i)+j < len(a) and a[(j-i)+j] =="G" and listg[j] >0:
                        count -=1
                    continue
                if a[i] =="G" and a[j] =="B":
                    count += listr[j]
                    if (j-i)+j < len(a) and a[(j-i)+j] =="R" and listr[j] >0:
                        count -=1
                    continue
                if a[i] =="G" and a[j] =="R":
                    count += listb[j]
                    if (j-i)+j < len(a) and a[(j-i)+j] =="B" and listb[j] >0:
                        count -=1
                    continue

                if a[i] =="B" and a[j] =="G":
                    count += listr[j]
                    if (j-i)+j < len(a) and a[(j-i)+j] =="R" and listr[j] >0:
                        count -=1
                    continue

                if a[i] =="B" and a[j] =="R":
                    count += listg[j]
                    if (j-i)+j < len(a) and a[(j-i)+j] =="G" and listg[j] >0:
                        count -=1
                    continue


    print(count)

Submission status: Time Limit Exceeded

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


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

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

    print(result)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:

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

count = 0
# i: 0～N-2
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[j] == S[k] or S[i] == S[k] or j == (k + i) / 2:
                continue
            else:
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
R,G,B = [0],[0],[0]
r,g,b = 0,0,0
ans = 0
for i in s[::-1]:
    if i == "R":
        r += 1
    elif i == "G":
        g += 1
    else:
        b += 1
    R.append(r)
    G.append(g)
    B.append(b)
R.reverse()
G.reverse()
B.reverse()
for ki,i in enumerate(s):
    for kj,j in enumerate(s[ki+1:],ki+1):
        INDEX = 2 * kj - ki
        if i == j:
            continue
        if i != "R" and j != "R":
            mini_ans = R[kj+1]
            if INDEX < n and s[INDEX] == "R":
                mini_ans -= 1
            ans += mini_ans
        elif i != "G" and j != "G":
            mini_ans = G[kj+1]
            if INDEX < n and s[INDEX] == "G":
                mini_ans -= 1
            ans += mini_ans
        elif i != "B" and j != "B":
            mini_ans = B[kj+1]
            if INDEX < n and s[INDEX] == "B":
                mini_ans -= 1
            ans += mini_ans
print(ans)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
S = input()
count = 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 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:
n = int(input())
s = input()
c = 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:
                c += 1
print(c)

Submission status: Time Limit Exceeded

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

t= [(S[a],S[b],S[c]) for a in range(N) for b in range(a+1, N) for c in range(b+1,N) if b-a != c-b and S[a] != S[b] and S[b] != S[c] and S[c]!= S[a]]
print(len(t))

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
        else:
            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 = count+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import product


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


def main():
    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)
    A = product(r, g, b)
    ans = 0
    for a in A:
        a = list(a)
        a.sort()
        if a[2] - a[1] != a[1] - a[0]:
            ans += 1
    print(ans)


if __name__ == "__main__":
    main()

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