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:
from itertools import product

n = int(input())
s = input()
r, g, b = [], [], []

for i, char in enumerate(s):
    if char == 'R': r.append(i)
    elif char == 'G': g.append(i)
    else: b.append(i)

count = 0
for config in product(r,g,b):
    a, b, c = config
    a, b, c = sorted((a, b, c))

    if (b-a)!=(c-b): count += 1

print(count)

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]
  for k in range(N-i-2):
   B3=A[i+2+k]
   if B1==B2 or B2==B3 or B1==B3 or i==j+k:
    continue
   else:
    c+=1
print(c)

Submission status: Time Limit Exceeded

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

def linput():
    return [int(x) for x in input().split()]

n = int(input())
s = input().rstrip()

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)

import bisect
from copy import deepcopy
ans = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] == s[j]:
            continue
        ng = j + (j - i)
        if s[i] == "R" and s[j] == "G":
            l = deepcopy(b)
        elif s[i] == "R" and s[j] == "B":
            l = deepcopy(g)
        elif s[i] == "G" and s[j] == "R":
            l = deepcopy(b)
        elif s[i] == "G" and s[j] == "B":
            l = deepcopy(r)
        elif s[i] == "B" and s[j] == "R":
            l = deepcopy(g)
        elif s[i] == "B" and s[j] == "G":
            l = deepcopy(r)
        ans += bisect.bisect_left(l, j + (j - i)) - bisect.bisect_right(l, j)
        ans += len(l) - bisect.bisect_right(l, j + (j - i))

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(N):
    x = S[i]
    for j in range(i+1,N):
        y = S[j]
        if x == y:
            continue
        else:
            for k in range(j+1,N):
                z = S[k]
                A = j - i
                B = k - j
                if x != z and y != z and (A != B):
                    ans += 1
                    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
line = input()
num = 0
t = []
for i in line:
    if i == 'R':
        t.append(1)
    elif i == 'G':
        t.append(2)
    else:
        t.append(4)
for i in range(N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            if t[i]+t[j]+t[k]==7:
                if abs(i-j) == abs(j-k):
                    continue
                num+=1
print(num)

Submission status: Time Limit Exceeded

Solution code:
def find_ijk(n,s):
    count = 0
    for i in range(0,n-2,1):
        a = s[i]
        for j in range(i+1,n-1,1):
            b = s[j]
            if a!=b:
                for k in range(j+1,n,1):
                    c = s[k]
                    if a!=c and b!=c:
                        if j-i!=k-j:
                            count+=1
    return count
                    

if __name__ == "__main__":
    n = int(input())
    s = input()
    pair = find_ijk(n,s)
    print(pair)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
preb=[0 for i in range(n)]
preg=[0 for i in range(n)]
prer=[0 for i in range(n)]
b=0; g=0; r=0
for i in range(n-1,-1,-1):
	if(s[i]=='R'):
		r+=1
	elif(s[i]=='G'):
		g+=1
	else:
		b+=1
	preb[i]=b; prer[i]=r; preg[i]=g
# print(preb)
# print(preg)
# print(prer)
l=[prer,preg,preb]
s=s.replace('R','0')
s=s.replace('G','1')
s=s.replace('B','2')
ans=0
# print(s)
for i in range(n):
	for j in range(i+1,n):
		if(s[i]!=s[j]):
			if(j+1>=n):
				continue
			ct = l[(3-int(s[i])-int(s[j]))][j+1]
			diff = j-i
			if((j+diff)<n and s[j+diff]==str(3-int(s[i])-int(s[j]))):
				ct-=1
			ans+=ct
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
ans = 0
r = [0 for i in range(n)]
g = [0 for i in range(n)]
b = [0 for i in range(n)]
for i in range(n):
    if s[i] == "R":
        r[i] += 1
    elif s[i] == "G":
        g[i] += 1
    else:
        b[i] += 1
    if i > 0:
        r[i] += r[i-1]
        g[i] += g[i-1]
        b[i] += b[i-1]
ju = [0,0,0]
ju2 = ["R","G","B"]
for i in range(n-2):
    for j in range(3):
        if s[i] == ju2[j]:
            ju[j] += 1
    for j in range(i+1,n-1):
        #print(ans)
        for k in range(3):
            if s[j] == ju2[k]:
                ju[k] += 1
                key = k
        if ju[key] == 1:
            if ju[0] == 0:
                ans += r[n-1] - r[j]
                if j + (j-i) < n:
                    if r[j+(j-i)] > r[j+(j-i)-1]:
                        ans -= 1
            
            elif ju[1] == 0:
                ans += g[n-1] - g[j]
                if j + (j-i) < n:
                    if g[j+(j-i)] > g[j+(j-i)-1]:
                        ans -= 1
            else:
                ans += b[n-1] - b[j]
                if j + (j-i) < n:
                    if b[j+(j-i)] > b[j+(j-i)-1]:
                        ans -= 1
        ju[key] -= 1
    ju = [0,0,0]
print(ans)

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 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:
NumString = int(input())
String = input()

total_ans = 0
for i in range(NumString):
    for j in range(i, NumString):
        for k in range(j, NumString):
            if String[i] != String[j] and String[j] != String[k] and \
                String[i] != String[k] and j - i != k - j:
                total_ans += 1

print(total_ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys
sys.setrecursionlimit(10000)
from functools import lru_cache

n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()

sum = 0

for i in range(len(s)):
    for j in range(i+1, len(s)):
        for k in range(j+1, len(s)):
            if (j-i) == (k-j):
                continue
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                sum += 1
print(sum)

Submission status: Time Limit Exceeded

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

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

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:
import itertools

def main():
    N = int(input())
    S = input()
    id_l = [i for i in range(N)]

    ans = 0
    for v in itertools.permutations(id_l, 3):
        if v[0] < v[1] < v[2]:
            if (v[2] - v[1]) != (v[1] - v[0]):
                if (S[v[0]] != S[v[1]]) and (S[v[1]] != S[v[2]]) and (S[v[0]] != S[v[2]]):
                    ans += 1
    print(ans)

    return 0


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import collections
from collections import deque
from functools import reduce
import heapq
n = int(input())
#n, k = map(int, sys.stdin.readline().split())
s = input()
#a = list(map(int, sys.stdin.readline().split()))


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 i in range(len(r)):
    for j in range(len(g)):
        d = abs(r[i] - g[j])
        e = 0
        p = min(r[i], g[j]) - d
        q = max(r[i], g[j]) + d
        if (r[i] + g[j]) % 2 == 0:
            t = int((r[i] + g[j]) / 2)
        else:
            t = 10000
        if p in b:
            e += 1
        if q in b:
            e += 1
        if t in b:
            e += 1

        ans += (len(b) - e)

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list_S = list(input())
list_R = []
list_B = []
list_G = []
ans = 0
for i, c in enumerate(list_S):
    if c == "R":
        list_R.append(i+1)
    if c == "B":
        list_B.append(i+1)
    if c == "G":
        list_G.append(i+1)

for i in list_R:
    for j in list_G:
        for k in list_B:
            l = [i,j,k]
            l.sort()
            if l[1]-l[0] != l[2]-l[1]:
                ans += 1

print(ans)

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

r = s.count("R")
g = s.count("G")
b = s.count("B")

ans = r * g * b


for i in range(n):
    for j in range(1, n // 2 + 1):
        if i-j < 0 or i+j > n-1:
            continue
        if s[i] != s[i-j] and s[i] != s[i+j] and s[i-j] != s[i+j]:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

import itertools
cnt=0
a=list(itertools.combinations(range(1,n+1),3))

for num in range(len(a)):
    i=int(a[num][0]-1)
    j=int(a[num][1]-1)
    k=int(a[num][2]-1)
    if j-i!=k-j:
        if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
            cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans=0
for i in range(N-2):
    now = S[i]
    for j in range(i+1,N-1):
        if S[j] == now:
            continue
        else:
            sec = S[j]
            for k in range(j+1,N):
                if S[k] == now or S[k] == sec:
                    continue
                else:
                    if j-i != k-j:
                        ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
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)
        x = abs(a[0] - a[1])
        y = abs(a[1] - a[2])
        z = abs(a[0] - a[2])
        if x != y and y != z and z != x:
            ans += 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s_li = list(s)
count = 0
for i in range(len(s_li)):
  for j in range(len(s_li)):
    for k in range(len(s_li)):
      if i < j < k <= n-1:
        if s_li[i] != s_li[j] and s_li[i] != s_li[k] and s_li[j] != s_li[k]:
          if j - i != k -j:
            count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N = I()
    s = S()
    d = collections.defaultdict(list)
    count = collections.defaultdict(set)
    for i, c in enumerate(s):
        d[c].append(i)
        count[c].add(i)
    cnt = 0
    for i, si in enumerate(s):
        for j, sj in enumerate(s):
            if i >= j:
                continue
            if si == sj:
                continue
            if si == 'R' and sj == 'G':
                targ = 'B'
            if si == 'R' and sj == 'B':
                targ = 'G'
            if si == 'G' and sj == 'R':
                targ = 'B'
            if si == 'G' and sj == 'B':
                targ = 'R'
            if si == 'B' and sj == 'R':
                targ = 'G'
            if si == 'B' and sj == 'G':
                targ = 'R'
            ix = bisect.bisect_left(d[targ], j+1)
            cnt += (len(d[targ]) - ix)
            if 2 * j - i in count[targ]:
                cnt -= 1
    print(cnt)
main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#create date: 2020-04-12 21:15

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    s = ns()
    r = s.count("R")
    g = s.count("G")
    b = s.count("B")
    ans = r*g*b
    for i in range(0, n-2):
        for j in range(1, (n-1)//+1):
            if i + 2*j < n:
                if len(set([s[i], s[i+j], s[i+2*j]]))==3:
                    ans -= 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import re

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

R = [m.start() for m in re.finditer('R', s)]
G = [m.start() for m in re.finditer('G', s)]
B = [m.start() for m in re.finditer('B', s)]

x=0
for r in R:
    for g in G:
        if r < g:
            gr = g-r
            if r-gr in B:
                x += 1
            if g+gr in B:
                x += 1
            if gr%2 == 0 and r+gr//2 in B:
                x += 1
        else:
            rg = r-g
            if g-rg in B:
                x += 1
            if r+rg in B:
                x += 1
            if rg%2 == 0 and g+rg//2 in B:
                x += 1
                
print(len(R)*len(G)*len(B)-x)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
L=len(s)
ans=0
R=[]
G=[]
B=[]
for i in range(L):
    if s[i]=='R':
        R.append(i)
    elif s[i]=='G':
        G.append(i)
    else:
        B.append(i)
for r in R:
    for g in G:
        for b in B:
            X=[r,b,g]
            X.sort()
            if X[1]-X[0]==X[2]-X[1]:
                continue
            ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
count=0
N=int(input())
slist=deque(list(input()))
for a in range(N-2):
    for b in range(a+1,N-1):
        for c in range(b+1,N):
            if slist[a]!=slist[b] and slist[b]!=slist[c] and slist[a]!=slist[c]:
                if b-a != c-b:
                    count+=1
print(count)

Submission status: Time Limit Exceeded

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

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

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

     ans = 0

     for i in range(n):
          if s[i] == 'R':
               r_list.append(i)
          elif s[i] == 'G':
               g_list.append(i)
          else:
               b_list.append(i)
     
     for r in r_list:
          for g in g_list:
               for b in b_list:
                    target = sorted([r, g, b])
                    if target[1] - target[0] != target[2] - target[1]:
                         ans += 1

     print(ans)

if __name__ == "__main__":
    main()

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

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

    cnt = 0
    for i, j, k in list(combinations(range(N), 3)):
        if j - i == k - j: continue
        if S[i] == S[j] or S[j] == S[k] or S[k] == S[i]: continue
        cnt += 1

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

count = 0

for i,first in enumerate(s[:-2]):
    for j,second in enumerate(s[i+1:n-1]):
        if first != second:
            for k,third in enumerate(s[i+j+2:]):
                if j != k:
                    if third != first and third != second:
                        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
for i, si in enumerate(s):
    if si == 'R':
        r.append(i)
    if si == 'G':
        g.append(i)
    if si == 'B':
        b.append(i)
sum_ = len(r) * len(g) * len(b)
for ri in r:
    for gi in g:
        if 2 * gi - ri in b:
            sum_ -= 1
        if 2 * ri - gi in b:
            sum_ -= 1
        if (ri + gi) / 2 in b:
            sum_ -= 1
print(sum_)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(n-3):
    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:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
S = input()
r = []
g = []
b = []
for i, s in enumerate(S):
    if s == "R":
        r.append(i)
    elif s == "G":
        g.append(i)
    else:
        b.append(i)

# print(s, r, g, b)
ans = 0
for i, j in enumerate(r):
    for k, l in enumerate(g):
        for m, n in enumerate(b):
            I = min(j, l, n)
            K = max(j, l, n)
            J = j + l + n - I - K
            if K - J != J - I:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math

import itertools
import heapq
#import bisect

import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)


def main():
    n=int(input())
    s=list(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)
    total=0
    for aaa1 in r:
        for aaa2 in g:
            for aaa3 in b:
                maxa=max([aaa1,aaa2,aaa3])
                mina=min([aaa1,aaa2,aaa3])
                meda=sum([aaa1,aaa2,aaa3])-maxa-mina
                if maxa-meda!=meda-mina:
                    total+=1
    print(total)



if __name__ == "__main__":

    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = [0]*N
G = [0]*N
B = [0]*N
ans = 0
for i in range(N):
    R[i] = R[i-1]
    G[i] = G[i-1]
    B[i] = B[i-1]
    if S[i] == 'R':
        R[i] += 1
    elif S[i] == 'G':
        G[i] += 1
    else:
        B[i] += 1
for i in range(N):
    for k in range(i,N):
        if (S[i] != S[k]) and (S[i] != 'R' and S[k] != 'R'):
            ans += R[N-1] - R[k]
            if k+(k-i) < N and S[k+(k-i)] == 'R':
                ans -= 1
        if (S[i] != S[k]) and (S[i] != 'G' and S[k] != 'G'):
            ans += G[N-1] - G[k]
            if k+(k-i) < N and S[k+(k-i)] == 'G':
                ans -= 1
        if (S[i] != S[k]) and (S[i] != 'B' and S[k] != 'B'):
            ans += B[N-1] - B[k]
            if k+(k-i) < N and S[k+(k-i)] == 'B':
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
k = 'RGB'
for i in range(N):
	for j in range(i+1,N):
		if not S[i]==S[j]:
			tmp =k.replace(S[i],'')
			tmp = tmp.replace(S[j],'')
			ans += S[j+1:].count(tmp)
			if 2*j-i<N and S[2*j-i]==tmp:
				ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, gcd
from functools import lru_cache, reduce
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# ここから書き始める
# k = int(input())
# ans = 0
# for i in range(1, k + 1):
#     for j in range(1, k + 1):
#         for l in range(1, k + 1):
#             # print("i =", i)
#             # print("j =",j)
#             # print("l =", l)
#             ans += gcd_list([i, j, l])   
# print(ans)

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 i in r:
    for j in g:
        for k in b:
            x, y, z = sorted([i, j, k])
            if y - x != z - y:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
s = input()
R = []
G = []
B = []
import bisect
def func(l1,l2,l3):
  res =0
  for i in l1:
    for j in l2:
      if i>j:
        continue
      a = l3[bisect.bisect_left(l3,j):]
      res += len(a)
      if j*2-i in a:
        res -=1
  return res

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 = func(R,G,B)+func(R,B,G)+func(G,R,B)+func(G,B,R) + func(B,R,G)+func(B,G,R)
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(n-2):
    s_i = s[i]
    for j in range(i+1, n-1):
        if s[j] == s_i: continue
        s_j = s[j]
        j_i = j-i
        for k in range(j+1, n):
            if k-j == j_i: continue
            if s[k] != s_i and s[k] != s_j:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
input = sys.stdin.readline

N = int(input())
S = input().strip()

s = 0
for i in range(N):
	for j in range(i+1, N):
		if S[j] == S[i]: continue
		dif = j - i
		for k in range(j+1, N):
			if S[j] == S[k] or S[k] == S[i]: continue
			if k-j == j-i: continue
			s += 1

print(s)

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 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:
from sys import stdin
import sys

# A, B, C = [int(x) for x in stdin.readline().rstrip().split()]
# A,B = [int(x) for x in stdin.readline().rstrip().split()]
# U = input().split()
# A, B = [str(x) for x in stdin.readline().rstrip().split()]
N = int(input())
S = list(input())

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