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


def main():
    n = int(input())
    s = input()
    comb = itertools.combinations(range(n), 3)

    def gen(comb):
        for cm in comb:
            a, b, c = cm
            if b - a == c - b:
                continue
            if len(set((s[a], s[b], s[c]))) == 3:
                yield True

    return sum(gen(comb))


if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = []
nsum = 0
for i in range(N):
    if S[i] == "R":
        R.append(i)
B = []
for i in range(N):
    if S[i] == "B":
        B.append(i)
g = 0
for i in range(N):
    if S[i] == "G":
        g += 1
        for r in R:
            for b in B:
                if abs(i - b) - abs(b - r) == 0 or abs(b - i) - abs(r - i) == 0 or abs(b - r) - abs(i - r) == 0:
                    nsum += 1
                    if max(i,b,r) == b: break
print(len(R)*len(B)*g - nsum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

# r[i] : s[i:]に何個あるか
r = [0] * (n + 1)
g = [0] * (n + 1)
b = [0] * (n + 1)

ss = s[::-1]
for i in range(n):
    c = ss[i]
    # if i > 0:
    #     r[i] = r[i - 1]
    #     g[i] = g[i - 1]
    #     b[i] = b[i - 1]

    r[i + 1] = r[i] + (c == 'R')
    g[i + 1] = g[i] + (c == 'G')
    b[i + 1] = b[i] + (c == 'B')

    # if ss[i] == 'R':
    #     r[i] += 1
    # if ss[i] == 'G':
    #     g[i] += 1
    # if ss[i] == 'B':
    #     b[i] += 1

r = r[::-1]
g = g[::-1]
b = b[::-1]

ans = 0



# rgb
for i in range(n):
    if s[i] != 'R':
        continue
    for j in range(i + 1, n):
        if s[j] != 'G':
            continue
        d = j - i
        ans += b[j]
        if j + d < n and s[j + d] == 'B':
            ans -= 1
# rbg
for i in range(n):
    if s[i] != 'R':
        continue
    for j in range(i + 1, n):
        if s[j] != 'B':
            continue
        d = j - i
        ans += g[j]
        if j + d < n and s[j + d] == 'G':
            ans -= 1
# grb
for i in range(n):
    if s[i] != 'G':
        continue
    for j in range(i + 1, n):
        if s[j] != 'R':
            continue
        d = j - i
        ans += b[j]
        if j + d < n and s[j + d] == 'B':
            ans -= 1
# gbr
for i in range(n):
    if s[i] != 'G':
        continue
    for j in range(i + 1, n):
        if s[j] != 'B':
            continue
        d = j - i
        ans += r[j]
        if j + d < n and s[j + d] == 'R':
            ans -= 1
# brg
for i in range(n):
    if s[i] != 'B':
        continue
    for j in range(i + 1, n):
        if s[j] != 'R':
            continue
        d = j - i
        ans += g[j]
        if j + d < n and s[j + d] == 'G':
            ans -= 1
# bgr
for i in range(n):
    if s[i] != 'B':
        continue
    for j in range(i + 1, n):
        if s[j] != 'G':
            continue
        d = j - i
        ans += r[j]
        if j + d < n and s[j + d] == 'R':
            ans -= 1

print(ans)

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)

R = len(r)
G = len(g)
B = len(b)

if min(R, G, B) == R:
    if max(R, G, B) == G:
        w = r
        x = b
        y = g
    else:
        w = r
        x = g
        y = b
elif min(R, G, B) == G:
    if max(R, G, B) == R:
        w = g
        x = b
        y = r
    else:
        w = g
        x = r
        y = b
else:
    if max(R, G, B) == R:
        w = b
        x = g
        y = r
    else:
        w = b
        x = r
        y = g


ans = 0
z = len(y)
for i in range(len(w)):
    for j in range(len(x)):
        d = abs(w[i] - x[j])
        e = 0
        p = min(w[i], x[j]) - d
        q = max(w[i], x[j]) + d
        if (w[i] + x[j]) % 2 == 0:
            t = int((w[i] + x[j]) / 2)
        else:
            t = 10000
        if p in y:
            e += 1
        if q in y:
            e += 1
        if t in y:
            e += 1

        ans += (z - e)

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())
    for i in range(n+3):
        s.append("A")
    for i in range(n):
        if s[i]=="R":
            s[i]=-1
        elif s[i]=="G":
            s[i]=1
        elif s[i]=="B":
            s[i]=0
    total=0
    for i in range(n-1):
        for j in range(i+1,n):
            if s[i]!=s[j]:
                if s[j*2-i]==(-1)*(s[i]+s[j]):
                    total-=1
                temp=s[j+1:]
                total+=temp.count((-1)*(s[i]+s[j]))


    print(total)



if __name__ == "__main__":

    main()

Submission status: Time Limit Exceeded

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


main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = input()
patterns = ["RGB","RBG","GBR","GRB","BRG","BGR"]
anss = [ [0] * (n + 1) for _ in range(n+1)]
acc  = [ [0] * (n + 1) for _ in range(n+1)]
ans = 0
for pattern in patterns:
    c1 = pattern[0]
    c2 = pattern[1]
    c3 = pattern[2]
    cands = []
    #i c1の位置で
    for i1 in range(n):
        for i2 in range(i1 + 1,n):
            if S[i1] == c2 and S[i2] == c3:
                cands.append((i1,i2-i1))
    # anss[i1][i2-i1]→左をi1に固定したときの、距離i2-i1の組の個数

    for  i in range(0,n):
        if S[i] == c1:
            for cand in cands:
                if  cand[0] - i  != cand[1] and cand[0] > i:
                    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):
        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()

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

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

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):
            if s[i] != s[j]:
                for k in range(j+1, n):
                    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:
import itertools
N = int(input())
S = input()
com=[]
li = com.append
com=[[i-1, j-1, k-1] for i in range(1, N+1) for j in range(i+1,N+1) for k in range(j+1, N+1) if( (j-i) != (k-j) and (S[i-1] != S[j-1]) and (S[j-1] != S[k-1]) and (S[k-1] != S[i-1]))]
print(len(com))

Submission status: Time Limit Exceeded

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

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

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

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N-2):
    for j in range(1,N-1):
        for k in range(2,N):
            d=j-i
            if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i] and (k-j)!=d and i<j<k:
                answer+=1
                #print(i,j,k)

                    
print(answer)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


def get_result(data):
    N = int(data[0][0])
    S = data[1][0]
    ans = 0
    for i in range(N-2):
        for j in range(i, N-1):
            for k in range(j, N):
                if (j-i) != (k-j):
                    if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                        ans += 1
    return ans


if __name__ == '__main__':
    raw_data = [val.rstrip() for val in stdin.readlines()]
    data = [list(map(str, val.split(' '))) for val in raw_data]
    result = get_result(data)
    print(result)

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

Submission status: Time Limit Exceeded

Solution code:
# =map(int,input().split())
n=int(input())
s=input()
import sys
cnt=0
for i in range(len(s)):
    first=s[i]
    for k in range(i+1,len(s)):
        if (s[k]!=first):
            second=s[k]
            for j in range(k+1,len(s)):
                if (s[j]!=first)and(s[j]!=second)and((j-k)!=(k-i)):
                    cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

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

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:
            if 'R' != first and 'R' != second:
                third = 'R'
            elif 'G' != first and 'G' != second:
                third = 'G'
            else:
                third = 'B'
            count += s[i+j+2:].count(third)
            if i+j+2+j <= n-1 and s[i+j+2:][j] == third:
                count -= 1
 
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from itertools import combinations

def gcd3(a, b , c):
	d = math.gcd(a, b)
	return math.gcd(d,c)

if __name__ == "__main__":
	N = int(input())
	S = list(input())


	sum = 0

	l = list(range(1, N+1))

	comb = list(combinations(l,3))
	for c in comb:
		if (S[c[0]-1] != S[c[1]-1]) and (S[c[0]-1] != S[c[2]-1]) and (S[c[1]-1] != S[c[2]-1]):
			if (c[1] - c[0]) != (c[2] - c[1]):
				sum += 1

	print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
#a,b = map(int,input().split())
#a_L = list(map(int,input().split()))

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

"""
for i in range(n):
    if n[i] == "R":
        R.append(i)
    elif n[i] == "G":
        G.append(i)
    else:
        B.append(i)
"""

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[i] != s[k] and s[k] != s[j]:
                if 2*j != i+k:
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    R = []
    G = []
    B = []
    N = int(input())
    S = input()
    for ind, ch in enumerate(S):
        if ch == 'R':
            R.append(ind)
        elif ch == 'B':
            B.append(ind)
        else:
            G.append(ind)
    total = 0
    for r in R:
        for g in G:
            for b in B:
                s_r, s_g, s_b = sorted([r, g, b])
                if s_g - s_r != s_b - s_g:
                    total += 1
    print(total)

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

Submission status: Time Limit Exceeded

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

char = [[] for _ in range(3)]

for i in range(N):
    s = S[i]
    if s == "R":
        char[0].append(i)
    elif s == "G":
        char[1].append(i)
    else:
        char[2].append(i)

ans = 0

for f, s, t in itertools.permutations(range(3), 3):
    for i in char[f]:
        # print(char[s])
        sec_start = bisect.bisect_left(char[s], i)
        for j in char[s][sec_start: ]:
            thi_start = bisect.bisect_left(char[t], j)
            for k in char[t][thi_start: ]:
                if j - i != k - j:
                    # print(i, j, k)
                    ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for a in range(N-2):
  for b in range(a+1,N-1):
    for c in range(b+1,N):
      if a-b == b-c:
        pass
      elif S[a] != S[b] and S[b] != S[c] and S[c] != S[a]:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
total = 0
a = ["R", "G", "B"]
for first in range(0, n-2):
	for second in range(first+1, n-1):
		if s[first] != s[second]:
			diff = second - first
			if s[first] == "R" and s[second] == "G":
				total += s[second+1:second+diff].count("B")
				total += s[second+diff+1:].count("B")
			elif s[first] == "R":
				total += s[second+1:second+diff].count("G")
				total += s[second+diff+1:].count("G")
			elif s[first] == "G" and s[second] == "B":
				total += s[second+1:second+diff].count("R")
				total += s[second+diff+1:].count("R")
			elif s[first] == "G":
				total += s[second+1:second+diff].count("B")
				total += s[second+diff+1:].count("B")
			elif s[first] == "B" and s[second] == "R":
				total += s[second+1:second+diff].count("G")
				total += s[second+diff+1:].count("G")
			elif s[first] == "B":
				total += s[second+1:second+diff].count("R")
				total += s[second+diff+1:].count("R")
		
print(total)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
count=0
S=input()
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[j]!=S[k]:
                    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+1)
    elif S[i] == 'G':
        G.append(i+1)
    elif S[i] == 'B':
        B.append(i+1)

ans = 0
for i in range(len(R)):
    for j in range(len(G)):
        ans += len(B)
        tmp_abs = abs(R[i]-G[j])

        if (min(R[i], G[j])-tmp_abs) in B:
            ans -= 1
        if (max(R[i], G[j])+tmp_abs) in B:
            ans -= 1
        if tmp_abs % 2 == 0 and (max(R[i], G[j])-(tmp_abs//2)) in B:
            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, n-1):
    for k in range(j, n):
      if k-j != j-i and s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
        res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
def have_c(c):
    if(c=='R'):
        return 4
    elif(c=='G'):
        return 2
    elif(c=='B'):
        return 1
    return 0

def have_n(n):
    if(n==4):
        return 'R'
    elif(n==2):
        return 'G'
    elif(n==1):
        return 'B'
    return 0
    
ans = 0
for i in range(N-3):
    for j in range(i+1,N-1):
        hav = have_c(S[i])
        if(hav == have_c(S[j])):
            continue
        hav += have_c(S[j])
        last_alpha = have_n(7-hav)
        for k in range(j+1,N):
            if(j-i == k-j):
                continue
            if(S[k]==last_alpha):
                ans +=1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    N = int(input())
    S = input()
    cnt = {'R': [], 'B': [], 'G': []}
    for i, c in enumerate(S):
        cnt[c].append(i)
    from itertools import permutations
    ans = 0
    for (ci, cj, ck) in permutations(['R', 'B', 'G']):
        for i in cnt[ci]:
            for j in cnt[cj]:
                if j < i:
                    continue
                for k in cnt[ck]:
                    if k < j or ((j - i) == (k - j)):
                        continue
                    ans += 1
    print(ans)
resolve()

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:
#!/usr/bin/env python

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

    rgb = {'r':[], 'g':[], 'b':[]}

    for i in range(n):
        if s[i] == 'R':
            rgb.get('r').append(i+1)
        elif s[i] == 'G':
            rgb.get('g').append(i+1)
        elif s[i] == 'B':
            rgb.get('b').append(i+1)

    count = 0
    for r_num in rgb.get('r'):
        for g_num in rgb.get('g'):
            for b_num in rgb.get('b'):
                i, j, k = tuple(sorted([r_num, g_num, b_num]))
                if j - i != k - j:
                    count += 1

    print(count)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(len(S)-2):
    char_1 = S[i]
    for j in range(i+1, len(S)-1):
        char_2 = S[j]
        if char_1 == char_2:
            continue
        one2twodist = j - i
        for k in range(j+1, len(S)):
            char_3 = S[k]
            if char_2 == char_3 or char_1 == char_3 or one2twodist == (k - j):
                continue
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
n=int(input())
s=str(input())
d=collections.defaultdict(list)
count=0
v=collections.defaultdict(int)
for i in range(n):
    d[s[i]].append(i)
for i in d["R"]:
    for j in d["G"]:
        if j>i:
            for k in d["B"]:
                if k>j:
                    if j-i!=k-j:
                        count+=1
for i in d["R"]:
    for j in d["B"]:
        if j>i:
            for k in d["G"]:
                if k>j:
                    if j-i!=k-j:
                        count+=1
for i in d["G"]:
    for j in d["R"]:
        if j>i:
            for k in d["B"]:
                if k>j:
                    if j-i!=k-j:
                        count+=1
for i in d["G"]:
    for j in d["B"]:
        if j>i:
            for k in d["R"]:
                if k>j:
                    if j-i!=k-j:
                        count+=1
for i in d["B"]:
    for j in d["R"]:
        if j>i:
            for k in d["G"]:
                if k>j:
                    if j-i!=k-j:
                        count+=1
for i in d["B"]:
    for j in d["G"]:
        if j>i:
            for k in d["R"]:
                if k>j:
                    if j-i!=k-j:
                        count+=1
                #v[(i,j,k)]+=1
print(count)
#print(v)
#print(len(v))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
X = [0] * N

for i in range(N):
    if S[i] == "R":
        X[i] = 1
    elif S[i] == "G":
        X[i] = 2
    elif S[i] == "B":
        X[i] = 3

Sum = 0

for i in range(N - 2):
    s1 = X[i]
    for j in range(i + 1, N - 1):
        s2 = X[j]
        if s1 != s2:
            for k in range(j + 1, N):
                if (6 - s1 - s2 == X[k]) and (j - i) != (k - j):
                    Sum += 1

print(Sum)

Submission status: Time Limit Exceeded

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

for i in range(n):
  if s[i] == "R":
    for j in range(n):
      if s[j] == "G":
        for k in range(n):
          if s[k] == "B":
            if float(k) == (i+j)/2 or float(i) == (j+k)/2 or float(j) == (k+i)/2:
              pass
            else:
              ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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

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

ans = len(r)*len(g)*len(b)

for _x1 in r:
    for _x2 in g:
        for _x3 in b:
            x = sorted([_x1, _x2, _x3])
            # print(0,_x1, _x2, _x3)
            m = x[1]
            if 3*m == sum(x):
                # print(1,_x1, _x2, _x3)
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

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

cnt = 0
for elem1 in r:
    for elem2 in g:
        for elem3 in b:
            lst = sorted([elem1, elem2, elem3])
            if (lst[1] - lst[0]) != (lst[2] - lst[1]):
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

R_inds = []
G_inds = []
B_inds = []

for i in range(n):
    if s[i] == 'R':
        R_inds.append(i)
    elif s[i] == 'G':
        G_inds.append(i)
    else:
        B_inds.append(i)
    
ans = 0
for r in R_inds:
    for g in G_inds:
        for b in B_inds:
            ls = [r,g,b]
            ls.sort()
            if ls[2] - ls[1] != ls[1] - ls[0]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

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

counter = 0
for p in range(n):
    for q in range(n):
        for r in range(n):
            if p >= q or q >= r:
                continue
            a = s[p]
            b = s[q]
            c = s[r]
            if a != b and b != c and c != a and r - q != q - p:
                # print(a, b, c, p, q, r)
                counter += 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
N =int(input())
S = str(input())
A = 0
R = []
G = []
B = []
for i in range(len(S)):
  if S[i] == "R":
    R.append(i+1)
  elif S[i] == "G":
    G.append(i+1)
  else:
    B.append(i+1)
#print(R)
#print(G)
#print(B)

for r in R:
  for g in G:
    for b in B:
      C = sorted([r,g,b])
      if C[1] - C[0] != C[2] - C[1]:
        A += 1
print(A)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = input()

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

print(count)

Submission status: Time Limit Exceeded

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

    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        for ib in range(ia+1, n-1):
            ta = s[ia]
            if s[ia] == s[ib]:
                pass
            else:
                sa = ib - ia
                cnt = 1
                ts = s[ib+1:]
                for ets in ts:
                    if cnt == sa:
                        pass
                    elif s[ib] != ets != ta:
                        r += 1
                    cnt += 1

    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=0
for i in range(n):
    si=s[i]
    for j in range(i+1,n):
        sj=s[j]
        if si==sj:
            pass
        else:
            for k in range(j+1,n):
                sk=s[k]
                if sk==si or sk==sj:
                    continue
                if j-i==k-j:
                    continue
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
a,b,c,count=0,0,0,0

for i in range(N):
	a=S[i]
	for h in range(i+1,N):
		b=S[h]
		if b==a:
			continue
		else:
			for g in range(h+1,N):
				c=S[g]
				if c==b or c==a or h-i == g-h:
					continue
				else:
					count+=1

print(count)

Submission status: Time Limit Exceeded

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

res = 0

for k in range(n):
    for j in range(k):
        for i in range(j):
            if s[i] != s[j] and s[i] != s[k] and s[j] != s[k] and (j+1) - (i+1) != (k+1) - (j+1):
                #print(f'({i+1},{j+1},{k+1})')
                res += 1
print(res)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
import sys

input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]

n=I()
s=input()
r=[]
g=[]
t=[]
for i in range(n):
    if s[i:i+1]=="R":
        r.append(i)
    elif s[i:i+1]=="G":
        g.append(i)
    elif s[i:i+1]=="B":
        t.append(i)
#print(r)
#print(g)
#print(t)
ans=len(r)*len(g)*len(t)
for a in r:
    for b in g:
        if 2*b-a in t:
            ans-=1
        if 2*a-b in t:
            ans-=1
        if (a+b)%2==0 and (a+b)//2 in t:
            ans-=1
print(ans)

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 collections import deque
def resolve():
    N = ir()
    S = [s for s in sr()]
    q = deque([])
    for i in range(N):
        q.append([S[i], S[i], i, -1])
    ans = 0
    while len(q) > 0:
        u, v, n, d = q.popleft()
        if d == -1:
            for i in range(n+1, N):
                if u != S[i]:
                    q.append([u, S[i], i, i-n])
        else:
            for i in range(n+1, N):
                if i == n+d:
                    continue
                if u != S[i] and v != S[i]:
                    ans += 1
    print(ans)
resolve()

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