Problem:


Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Time Limit Exceeded

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

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

print(d)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

R, G, B = deque(), deque(), deque()
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
while len(R) != 0:
    r = R.popleft()
    for i in range(len(G)):
        g = G[i]
        for j in range(len(B)):
            L = sorted([r, g, B[j]])
            if L[2] - L[1] != L[1] - L[0]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

ans = r * g * b

i = 0
p = 1
while i + p + p < len(s):
    j = 0
    while j + p + p < len(s):
        if s[j] != s[j+p] and s[j+p] != s[j+p+p] and s[j] != s[j+p+p]:
            ans -= 1
        j += 1
    p += 1
"""
for i in range(n-2):
    if s[i] == "R":
        for j in range(i, n-1):
            if s[j] == "G":
                for k in range(j, n):
                    if s[k] == "B":
                        #if j - i != k - j:
                        ans += 1
            if s[j] == "B":
                for k in range(j, n):
                    if s[k] == "G":
                        #if j - i != k - j:
                        ans += 1
    
    if s[i] == "G":
        for j in range(i, n-1):
            if s[j] == "R":
                for k in range(j, n):
                    if s[k] == "B":
                        #if j - i != k - j:
                        ans += 1
            if s[j] == "B":
                for k in range(j, n):
                    if s[k] == "R":
                        #if j - i != k - j:
                        ans += 1
    if s[i] == "B":
        for j in range(i, n-1):
            if s[j] == "R":
                for k in range(j, n):
                    if s[k] == "G":
                        #if j - i != k - j:
                        ans += 1
            if s[j] == "G":
                for k in range(j, n):
                    if s[k] == "R":
                        #if j - i != k - j:
                        ans += 1

for i in range(n-2):
    if s[i] == "R":
        for j in range(i, n-1):
            if s[j] == "B":
                for k in range(j, n):
                    if s[k] == "G":
                        if j - i != k - j:
                            ans += 1

for i in range(n-2):
    if s[i] == "G":
        for j in range(i, n-1):
            if s[j] == "R":
                for k in range(j, n):
                    if s[k] == "B":
                        if j - i != k - j:
                            ans += 1

for i in range(n-2):
    if s[i] == "G":
        for j in range(i, n-1):
            if s[j] == "B":
                for k in range(j, n):
                    if s[k] == "R":
                        if j - i != k - j:
                            ans += 1

for i in range(n-2):
    if s[i] == "B":
        for j in range(i, n-1):
            if s[j] == "R":
                for k in range(j, n):
                    if s[k] == "G":
                        if j - i != k - j:
                            ans += 1

for i in range(n-2):
    if s[i] == "B":
        for j in range(i, n-1):
            if s[j] == "G":
                for k in range(j, n):
                    if s[k] == "R":
                        if j - i != k - j:
                            ans += 1
"""
print(ans)

Submission status: Time Limit Exceeded

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

R_list=[]
G_list=[]
B_list=[]
daburi = 0

for i in range(n):
    if s.find('R') == 0:
        R_list.append(i)
    elif s.find('G') == 0:
        G_list.append(i)
    else:
        B_list.append(i)
    s = s[1:]

for j in range(len(R_list)):
    for k in range(len(G_list)):
        for l in range(len(B_list)):
            if abs(R_list[j] - G_list[k]) == abs(G_list[k]- B_list[l]) or abs(R_list[j] - G_list[k]) == abs(R_list[j] - B_list[l]) or abs(G_list[k]- B_list[l]) == abs(R_list[j]- B_list[l]) :
                daburi +=1

ans = len(R_list)*len(G_list)*len(B_list)
print(ans - daburi)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

def myindex(List,liter):
    index_L = []
    for val in range(0,len(List)):
        if liter == List[val]:
            index_L.append(val)
    return index_L

def scal(a,b,c):
  if a < b < c:
    return a,b,c
  elif c < b < a:
    return c,b,a
  elif b < c < a:
    return b,c,a
  elif a < c < b:
    return a,c,b
  elif b < a < c:
    return b,a,c
  elif c < a < b:
    return c,a,b
  
R = myindex(S,"R")
G = myindex(S,"G")
B = myindex(S,"B")

t = list(itertools.product(R, G, B))
total=0
for x in t:
  i,j,k = scal(x[0],x[1],x[2])
  if j*2 == i+k:
    total += 1
print(len(t)-total)

Submission status: Time Limit Exceeded

Solution code:

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

count = 0

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

print(count)

Submission status: Time Limit Exceeded

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

r = 0
g = 0
b = 0
ans = 0
for s in S:
    if s == "R":
        r += 1
    elif s == "G":
        g += 1
    else:
        b += 1

# print(r, g, b)

for i in range(len(S)-2):
    if S[i] == "R":
        # if not (S[i + 1] == "G" and S[i + 2] == "B") or (S[i + 1] == "B" and S[i + 2] == "G"):
            # ans -= 1
        ans += g * b
        r -= 1
    elif S[i] == "G":
        # if not(S[i+1] == "R" and S[i+2] == "B") or (S[i+1] == "B" and S[i+2] == "R"):
            # ans -= 1
        ans += r * b
        g -= 1
    else:
        # if not(S[i+1] == "G" and S[i+2] == "R") or (S[i+1] == "R" and S[i+2] == "G"):
        #     ans -= 1
        ans += r * g
        b -= 1

for i in range(len(S)):
    for j in range(len(S)):
        j += 1
        if i + j >= len(S) or i + j + j >= len(S):
            break
        if S[i] == "R":
            if S[i + j] == "G" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "G":
                ans -= 1
        if S[i] == "G":
            if S[i + j] == "R" and S[i + j + j] == "B" or S[i + j] == "B" and S[i + j + j] == "R":
                ans -= 1
        if S[i] == "B":
            if S[i + j] == "G" and S[i + j + j] == "R" or S[i + j] == "R" and S[i + j + j] == "G":
                ans -= 1
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,N):
        x,y=S[i],S[j]
        if not x==y:
            for k in range(j,N):
                if not 2*j-i==k:
                    z=S[k]
                    if not x==z:
                        if not y==z:
                            ans+=1
print(ans)

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()

    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(count)


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=0
#a=min(s.count("R"),s.count("G"),s.count("B"))
for i in range(n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            if s[j]!=s[i] and s[i]!=s[k] and s[j]!=s[k] and j-i!=k-j:
                ans = ans+1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(N-2):
    for k in range(i+3, N):
        if S[i] != S[k]:
            A = ["R", "G", "B"]
            A.remove(S[i])
            A.remove(S[k])
            ans += S[i+1:k].count(A[0])
            if (k-i) % 2 == 0:
                if S[int((k-i)/2 + i)] == A[0]:
                    ans -= 1


print(ans)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(N):
    # if not S[i] == 'R': continue
    for j in range(i+1, N):
        if S[i] != S[j]:
            for k in range(j+1, N):
                if S[i] != S[k] and S[j] != S[k]:
                    if j - i != k - j:
                        count += 1
                    else:
                        continue
                else:
                    continue

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
r = []
g = []
b = []


for x in range(n):
    if s[x] == 'R':
        r.append(x+1)
        continue
    if s[x] == 'G':
        g.append(x+1)
        continue
    if s[x] == 'B':
        b.append(x+1)
        continue

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:
n = int(input())
s = input()

r_list = []
g_list = []
b_list = []

res = 0

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

# print(r_list)
# print(g_list)
# print(b_list)

# print(len(r_list)*len(g_list)*len(b_list))
res = len(r_list)*len(g_list)*len(b_list)
for r_index in r_list:
    for g_index in g_list:
        for b_index in b_list:
            tmp = sorted([r_index, g_index, b_index])
            if tmp[2]-tmp[1] == tmp[1]-tmp[0]:
                res -= 1
print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from bisect import bisect_left
def resolve():
    N = ir()
    S = sr()
    R = []
    G = []
    B = []
    for i in range(N):
        if S[i] == 'R':
            R.append(i)
        elif S[i] == 'G':
            G.append(i)
        else:
            B.append(i)
    ans = 0
    for n in R:
        for i in G:
            if i < n:
                continue
            x = bisect_left(B, i)
            ans += len(B[x:])
            if 2*i-n in B:
                ans -= 1
    for n in R:
        x = bisect_left(B, n)
        for i in B:
            if i < n:
                continue
            x = bisect_left(G, i)
            ans += len(G[x:])
            if 2*i-n in G:
                ans -= 1
    for n in G:
        x = bisect_left(R, n)
        for i in R:
            if i < n:
                continue
            x = bisect_left(B, i)
            ans += len(B[x:])
            if 2*i-n in B:
                ans -= 1
    for n in G:
        x = bisect_left(B, n)
        for i in B:
            if i < n:
                continue
            x = bisect_left(R, i)
            ans += len(R[x:])
            if 2*i-n in R:
                ans -= 1
    for n in B:
        x = bisect_left(R, n)
        for i in R:
            if i < n:
                continue
            x = bisect_left(G, i)
            ans += len(G[x:])
            if 2*i-n in G:
                ans -= 1
    for n in B:
        x = bisect_left(G, n)
        for i in G:
            if i < n:
                continue
            x = bisect_left(R, i)
            ans += len(R[x:])
            if 2*i-n in R:
                ans -= 1
    print(ans)
resolve()

Submission status: Time Limit Exceeded

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

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)

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

print(len(R) * len(G) * len(B) - dif)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

# 標準入力 <int>
N = int(input())

# 標準入力<str>
S = input()

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

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

ri = -1
gi = -1
bi = -1

count =  0

while True:
    ri = s.find('R', ri+1)
    if ri == -1:
        break
    while True:
        gi = s.find('G', gi+1)
        if gi == -1:
            break
        while True:
            bi = s.find('B', bi+1)
            if bi == -1:
                break
            l = sorted([ri, gi, bi])
            if l[2] - l [1] != l[1] - l[0]:
                count += 1

print(count)

Submission status: Time Limit Exceeded

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

result = 0

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

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

for r in R:
    for b in B:
        for g in G:
            rb = abs(r-b)
            rg = abs(r-g)
            bg = abs(b-g)
            if rb != rg and rb != bg and rg != bg:
                result += 1

print(result)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
S = input()
# ans = 0
R_index = [n for n, v in enumerate(S) if v == 'R']
G_index = [n for n, v in enumerate(S) if v == 'G']
B_index = [n for n, v in enumerate(S) if v == 'B']
ans = len(R_index) *len(G_index) *len(B_index)
for i in range(N):
    for j in range(i+1, N):
        if S[i] == 'R' and S[j] =='G':
            # ans += sum(x > j for x in B_index)
            if j+j-i in B_index:
                ans -= 1
            continue
        elif S[i] == 'R' and S[j] =='B':
            # ans += sum(x > j for x in G_index)
            if j+j-i in G_index:
                ans -= 1
            continue
        elif S[i] == 'G' and S[j] =='R':
            # ans += sum(x > j for x in B_index)
            if j+j-i in B_index:
                ans -= 1
            continue
        elif S[i] == 'G' and S[j] =='B':
            # ans += sum(x > j for x in R_index)
            if j+j-i in R_index:
                ans -= 1
            continue
        elif S[i] == 'B' and S[j] =='R':
            # ans += sum(x > j for x in G_index)
            if j+j-i in G_index:
                ans -= 1
            continue
        elif S[i] == 'B' and S[j] =='G':
            # ans += sum(x > j for x in R_index)
            if j+j-i in R_index:
                ans -= 1
            continue
        # 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)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from collections import deque 
N = int(input())
S = input()
ans = 0

def dfs(i):
    cnt = 0
    ls = []
    for j in range(i+1,N):
        if S[j] != S[i]:
            ls.append(j)
    for k in ls:
        for l in range(k+1,N):
            if S[k] != S[l] and S[k] != S[i] and S[l] != S[i] and(l-k != k-i):
                cnt += 1
    return cnt

for m in range(N):
    ans += dfs(m)
        
print(ans)

Submission status: Time Limit Exceeded

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

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

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

ans = 0
for r in R:
    for g in G:
        for b in B:
            i, k = min(r, g, b), max(r, g, b)
            j = r+g+b-i-k
            if j-i!=k-j:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n = int(input())
s = list(str(input()))
z = 0
lnr = [i for i, x in enumerate(s) if x == 'R']
lng = [i for i, x in enumerate(s) if x == 'G']
lnb = [i for i, x in enumerate(s) if x == 'B']     
for i in lnr:
    for j in lng:
        for k in lnb:
            
            ln = sorted([i, j, k])
            if ln[0] - ln[1] == ln[1] - ln[2]:
                z += 1
print(len(lnr) * len(lng) * len(lnb) - z)

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations, product

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

R_indices = [i for i in range(N) if S[i] == "R"]
G_indices = [i for i in range(N) if S[i] == "G"]
B_indices = [i for i in range(N) if S[i] == "B"]

count = 0
for indices_1, indices_2, indices_3 in permutations([R_indices, G_indices, B_indices]):
    j_idx_prev = -1
    for i in indices_1:
        j_idx = len(indices_2)
        ng = j_idx_prev
        while j_idx - ng > 1:
            m = (j_idx + ng) // 2
            if i < indices_2[m]:
                j_idx = m
            else:
                ng = m

        j_idx_prev = j_idx - 1
        k_idx_prev = -1
        for j in indices_2[j_idx:]:
            k_idx = len(indices_3)
            ng = k_idx_prev
            while k_idx - ng > 1:
                m = (k_idx + ng) // 2
                if j < indices_3[m]:
                    k_idx = m
                else:
                    ng = m

            k_idx_prev = k_idx - 1
            for k in indices_3[k_idx:]:
                if j - i != k - j:
                    count += 1

print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

s = input()

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

a = 0


for x in r:
    for y in b:
        for z in g:
            i, j, k = sorted([x, y, z])
            if j - i != k - j:
                a += 1

print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from functools import reduce

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

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

N = II()
S = input()

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

Submission status: Time Limit Exceeded

Solution code:
import sys

N = int(sys.stdin.readline().rstrip())
S = sys.stdin.readline().rstrip()

if len(set(S)) == 1 or N < 3:
    print(0)
    sys.exit()

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

ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        for k in range(j + 1, N):
            if check(i, j, k):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin, stdout
def main():
    N=int(stdin.readline())
    S=stdin.readline().rstrip()
    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 ri in R:
        for gi in G:
            for bi in B:
                if abs(gi-ri) != abs(bi-gi) and abs(ri-bi) != abs(bi-gi) and abs(ri-bi) != abs(ri-gi):
                    cnt+=1

    stdout.write(str(cnt)+"\n")

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = input()
    r = 0
    s = [ord(l) for l in s]
    for k in range(1, n):
        sk = s[k]
        for j in range(1, k):
            sj = s[j]
            if sj != sk:
                for i in range(j):
                    si = s[i]
                    if j - i != k - j and si != sk and si != sj:
                        r += 1
    print(r)


main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
Sl = list(S)
Nl = [int(x) for x in range(1,N+1)]
res =0
for (i,Ni) in zip(Sl,Nl):
    x = i
    Nx = Ni
    Sl2 = Sl[Nx:]
    Nl2 = Nl[Nx:]
    for (j,Nj) in zip(Sl2,Nl2):
        y = j
        if y == x:
            pass
        else:
            Ny =Nj 
            Sl3 =Sl[Ny:]
            Nl3 =Nl[Ny:]
            for (k,Nk) in zip(Sl3,Nl3):
                z = k
                if z ==y:
                    pass
                elif z == x:
                    pass
                else:
                    Nz = Nk
                    Ma = Nz-Ny
                    Mb = Ny-Nx
                    if Ma != Mb:
                        res =res+1
print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
rgb = 'RGB'
cnt=0
for si in rgb:
  for sj in rgb:
    for sk in rgb:
      if not(si == sj) and not(sj==sk) and not(si==sk):
        si_list = [n for n, v in enumerate(S) if v == si]
        sj_list = [n for n, v in enumerate(S) if v == sj]
        sk_list = [n for n, v in enumerate(S) if v == sk]
        for i in si_list:
          for j in sj_list:
            for k in sk_list:
              if k > j and j > i and not(k-j == j-i):
                cnt +=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
if(n<3):
    print(0)
    exit()
sum=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):
                    sum+=1
print(sum)

Submission status: Time Limit Exceeded

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

tmp=0
C = itertools.combinations(S, 3)
for c in C:
    if set(c)==set('RGB'):
        tmp+=1
for j in range(1, N//2+1):
    for i in range(N):
        if (i+(j*2))>=N:
            break
        if set((S[i], S[i+j], S[i+(j*2)])) == set('RGB'):
#             print(i, i+j, i+(j*2))
            tmp-=1
print(tmp)

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 fractions import gcd
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 functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    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)
        else: B.append(i + 1)

    cnt = 0
    for r in R:
        for g in G:
            for b in B:
                i = min(r, g, b)
                k = max(r, g, b)
                for x in [r, g, b]:
                    if i < x < k: j = x
                if j - i != k - j: cnt += 1

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
L_r = []
L_g = []
L_b = []
ans = 0
for i in range(N):
    if S[i] == "R":
        L_r.append(i)
    elif S[i] == "G":
        L_g.append(i)
    else:
        L_b.append(i)
m = min(len(L_r),len(L_g),len(L_b))
if len(L_r) == m:
    for x in L_g:
        for y in L_b:
            ans += m
            if 2*x - y in L_r:
                ans -= 1
            if 2*y - x in L_r:
                ans -= 1
            if (x + y) % 2 == 0 and (x + y) // 2 in L_r:
                ans -= 1
elif len(L_g) == m:
    for x in L_r:
        for y in L_b:
            ans += m
            if 2*x - y in L_g:
                ans -= 1
            if 2*y - x in L_g:
                ans -= 1
            if (x + y) % 2 == 0 and (x + y) // 2 in L_g:
                ans -= 1
else:
    for x in L_r:
        for y in L_g:
            ans += m
            if 2*x - y in L_b:
                ans -= 1
            if 2*y - x in L_b:
                ans -= 1
            if (x + y) % 2 == 0 and (x + y) // 2 in L_b:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

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

RGB = list(S)
count = 0
for a in range(1, N-1):
    for b  in range(a+1, N):
        if not RGB[a-1] == RGB[b-1]: 
            for c in range(b+1, N+1):
                if RGB[b-1] != RGB[c-1] and RGB[c-1] != RGB[a-1]:
                    if not b-a == c-b:
                        count +=1
print(count)

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)

t=0
k=0
A=len(r)
B=len(g)
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*len(b)-k)