Problem:


Score : 400 points

Problem Statement

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

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

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

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

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

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

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

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Time Limit Exceeded

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

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

for i in range(n):
  if (s[i] == 'R'):
    r.append(i)
  elif (s[i] == 'G'):
    g.append(i)
  else:
    b.append(i)
    
cnt = 0
for i in r:
  for j in g:
    for k in b:
      if (((j-i) != (k-j)) and (i<j) and (j<k)):
      	cnt += 1
for i in r:
  for j in b:
    for k in g:
      if (((j-i) != (k-j)) and (i<j) and (j<k)):
      	cnt += 1
for i in g:
  for j in b:
    for k in r:
      if (((j-i) != (k-j)) and (i<j) and (j<k)):
      	cnt += 1
for i in g:
  for j in r:
    for k in b:
      if (((j-i) != (k-j)) and (i<j) and (j<k)):
      	cnt += 1
for i in b:
  for j in r:
    for k in g:
      if (((j-i) != (k-j)) and (i<j) and (j<k)):
      	cnt += 1
for i in b:
  for j in g:
    for k in r:
      if (((j-i) != (k-j)) and (i<j) and (j<k)):
      	cnt += 1
        
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = 0
for i in range(N):
    for j in range(i,N):
        if not S[i]==S[j]:
            for k in range(j,N):
                if not ((S[i]==S[k] or S[j]==S[k]) or j-i==k-j):
                    ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(str(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 r in R:
	for g in G:
		ans+=len(B)
		if (2*g-r) in B:
			ans-=1
		if (2*r-g) in B:
			ans-=1
		if (g+r)/2 in B:
			ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
R = []
G = []
B = []
for i, s in enumerate(S):
    if s == 'R':
        R.append(i+1)
    elif s == 'G':
        G.append(i+1)
    else:
        B.append(i+1)
res = []
# RGB
for a in R:
    for b in [b for b in G if b > a]:
        for c in [c for c in B if c > b]:
            res.append([a, b, c])
# RBG
for a in R:
    for b in [b for b in B if b > a]:
        for c in [c for c in G if c > b]:
            res.append([a, b, c])
# BRG
for a in B:
    for b in [b for b in R if b > a]:
        for c in [c for c in G if c > b]:
            res.append([a, b, c])
# BGR
for a in B:
    for b in [b for b in G if b > a]:
        for c in [c for c in R if c > b]:
            res.append([a, b, c])
# GRB
for a in G:
    for b in [b for b in R if b > a]:
        for c in [c for c in B if c > b]:
            res.append([a, b, c])
# GBR
for a in G:
    for b in [b for b in B if b > a]:
        for c in [c for c in R if c > b]:
            res.append([a, b, c])
print(len([x for x in res if x[1] - x[0] != x[2] - x[1]]))

Submission status: Time Limit Exceeded

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

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

print(total)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
N=int(input())
S=list(input())
Pattern=[x for x in combinations(range(N),3) if (len(set(S[y] for y in x))==3 and x[2]-x[1] != x[1]-x[0])]
print(len(Pattern))

Submission status: Time Limit Exceeded

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

import sys
import math

debug = False

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


def parse_input(lines_as_string = None):

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

    return (n, s)


def solve(n, s):

    result = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            for k in range(j+1, n):
                if j - i == k - j:
                    continue

                if s[i] != s[j] and s[i] != s[k] and s[j] != s[k]:
                    result = result + 1
    
    return result

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

if __name__ == '__main__':

    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
s = list(input())
r = [0] * n
g = [0] * n
b = [0] * n
for i in range(n):
    if s[i] == 'R':
        r[i] = 1
    elif s[i] == 'G':
        g[i] = 1
    else:
        b[i] = 1
cr = np.cumsum(r)
cg = np.cumsum(g)
cb = np.cumsum(b)
ans = 0
for i in range(n-2):
    a = s[i]
    for j in range(i+1, n-1):
        b = s[j]
        if a == b:
            continue
        diff = j - i
        c = {'R', 'G', 'B'} - {a, b}
        c = list(c)[0]
        if c == 'R':
            ans += cr[-1] - cr[j]
        elif c == 'G':
            ans += cg[-1] - cg[j]
        else:
            ans += cb[-1] - cb[j]
        if j + diff < n:
            if s[j + diff] == c:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:






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

for i in range(len(s)):
	if s[i] == 'R':
		r.append(i)
	elif s[i] == 'B':
		b.append(i)
	else:
		g.append(i)

for i in range(len(s)):
	if s[i] == 'R':
		for j in b:
			if i <= j:
				continue
			else :
				for k in g:
					if k <= j and j-i != k-j:
						total += 1
		for j in g:
			if i <= j:
				continue
			else :
				for k in b:
					if k <= j and j-i != k-j:
	
						total += 1

	elif s[i] == 'B':
		for j in r:
			if i <= j:
				continue
			else :
				for k in g:
					if k <= j and j-i != k-j:
	
						total += 1
		for j in g:
			if i <= j:
				continue
			else :
				for k in r:
					if k <= j and j-i != k-j:
	
						total += 1
	else:
		for j in b:
			if i <= j:
				continue
			else :
				for k in r:
					if k <= j and j-i != k-j:
	
						total += 1

		for j in r:
			if i <= j:
				continue
			else :
				for k in b:
					if k <= j and j-i != k-j:
	
						total += 1

print(total)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
if sys.platform =='ios':
    sys.stdin=open('input_file.txt')
    
n=int(input())
a=input()

cnt=0
r=[]
g=[]
b=[]
for i in range(n):
	if a[i]=="R":
		r.append(i)
	if a[i]=="B":
		b.append(i)
	if a[i]=="G":
		g.append(i)


for i1 in r:
	for i2 in b:
		for i3 in g:
			if abs(i1-i2)==abs(i2-i3) or abs(i1-i3)==abs(i2-i3) or abs(i1-i3)==abs(i2-i1):
				cnt+=0
			else:
				cnt+=1
			
			
print(cnt)

Submission status: Time Limit Exceeded

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

ridx,gidx,bidx=[],[],[]
for i,s in enumerate(S):
  if s=='R': ridx.append(i)
  if s=='G': gidx.append(i)
  if s=='B': bidx.append(i)
ans=0
for r in ridx:
  for g in gidx:
    for b in bidx:
      tmp=sorted([r,g,b])
      i,j,k=tmp[0],tmp[1],tmp[2]
      if i<j and j<k:
        if j-i!=k-j:
          ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

r_index_lis, g_index_lis, b_index_lis = [], [], []
for i, s in enumerate(S):
    if s == 'R':
        r_index_lis.append(i)
    elif s == 'G':
        g_index_lis.append(i)
    else:
        b_index_lis.append(i)
count = 0
for r in r_index_lis:
    for g in g_index_lis:
        for b in b_index_lis:
            rgb = sorted([r, g, b])
            if (rgb[0] - rgb[1] != rgb[1] - rgb[2]):
                count += 1
print(count)

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(n):
  for j in range(i+1, n):
    if s[i] != s[j]:
      for k in range(j+1, n):
        if 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()
count=0
nocount=0
Rcount=0
Gcount=0
Bcount=0
for k in range(N):
    if S[k]=="R":
        Rcount+=1
    if S[k]=="G":
        Gcount+=1
Bcount = N -Rcount-Gcount
count=Rcount*Gcount*Bcount
for k in range(3,N+1):
    for i in range(1,k-1):
        if k%2==i%2 and S[i-1]!=S[k-1]:
            j=(i+k)//2
            if S[i-1]!=S[j-1] and S[k-1]!=S[j-1]:
                nocount+=1
print(count-nocount)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
dp = [0] * 4000
dp[0] = 0

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

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

R=list()
G=list()
B=list()

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

sum=0
l=0
m=0
n=0
for i in range(N):
    if S[i]=='R':
        for p in G[m:]:
            for q in B[n:]:
                r=min(p,q)
                s=max(p,q)
                if (s-r != r-i):
                    sum+=1
        l+=1
    elif S[i]=='G':
        for p in R[l:]:
            for q in B[n:]:
                r=min(p,q)
                s=max(p,q)
                if (s-r != r-i):
                    sum+=1
        m+=1
    else:
        for p in R[l:]:
            for q in G[m:]:
                r=min(p,q)
                s=max(p,q)
                if (s-r != r-i):
                    sum+=1
        n+=1

print(sum)

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())
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)
# print(r)
# print(g)
# print(b)
cnt = 0
for i in range(len(r)):
    for j in range(len(g)):
        for k in range(len(b)):
            if r[i] != min(r[i], g[j], b[k]) and r[i] != max((r[i], g[j], b[k])):
                if r[i]-min(r[i], g[j], b[k]) != max((r[i], g[j], b[k]))-r[i]:
                    cnt += 1
            elif g[j] != min(r[i], g[j], b[k]) and g[j] != max((r[i], g[j], b[k])):
                if g[j]-min(r[i], g[j], b[k]) != max((r[i], g[j], b[k]))-g[j]:
                    cnt += 1
            else:
                if b[k]-min(r[i], g[j], b[k]) != max((r[i], g[j], b[k]))-b[k]:
                    cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    # N = 39
    # S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
    
    # ######### Index
    # R, G, B = [], [], []
    # for i, s in enumerate(S):
    #     if s == 'R':
    #         R.append(i)
    #     elif s == 'G':
    #         G.append(i)
    #     elif s == 'B':
    #         B.append(i)
    
    C = 0
    out = 0
    # for i in R:
    # for i in B:
    for i in range(len(S)):
        
        
        # for j in G:
        # for j in B:
        for j in range(i+1, len(S)):
            

            # for k in B:
            # for k in G:
            # for k in R:
            for k in range(j+1, len(S)):
                
                # print(i, j, k)
                
                if not (j - i) == (k - j):
                    if (S[i] != S[j]) and (S[i] != S[k]) and (S[k] != S[j]):
                    
                        
                        out += 1


    print(out)

    
main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
 
x1 = int(input())
x2 = input()
RGB = {"R", "G", "B"}
_sum = 0
x = [1 for i, rgb in zip(itertools.combinations(range(x1), 3), itertools.combinations(x2, 3)) if set(rgb) == RGB and (i[1] - i[0]) != (i[2] - i[1])]
print(sum(x))

Submission status: Time Limit Exceeded

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

ss = [0] * n
ans = 0
for i in range(n):
    if s[i] == 'R':
        ss[i] = 1
    elif s[i] == 'G':
        ss[i] = 2
    else:
        ss[i] = 3

for i in range(n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            if (j - i) == (k - j):
                continue
            if ss[i] == ss[j] == ss[k] == 2:
                continue
            else:
                if ss[i] + ss[j] + ss[k] == 6:
                    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

R_index = my_index_multi(s,'R')
G_index = my_index_multi(s,'G')
B_index = my_index_multi(s,'B')

for i in range(len(R_index)):
    R_index[i] += 1
for j in range(len(G_index)):
    G_index[j] += 1
for k in range(len(B_index)):
    B_index[k] += 1

count = 0
for i in range(len(R_index)):
    for j in range(len(G_index)):
        for k in range(len(B_index)):
            li = [R_index[i],G_index[j],B_index[k]]
            li.sort()
            if (li[1]*2 != li[0]+li[2]):
                count = count +1

#print(R_index)
#print(G_index)
#print(B_index)
print(count)

Submission status: Time Limit Exceeded

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

ans=0
i=0
while i<N-2:
    j=i+1
    while j<N-1:
        k=j+1
        while k<N:
            if S[i]==S[j]:
                break
            ans+=1*(S[j]!=S[k])*(S[k]!=S[i])*(j-i!=k-j)
            k+=1
        j+=1
    i+=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]:count=count+1
   elif s[i]==s[k]:count=count+1
   elif s[j]==s[k]:count=count+1
   elif j-i==k-j:count=count+1
print(p-count)

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

Rtmp=[]
Gtmp=[]
Btmp=[]


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

if len(Rtmp) >= len(Gtmp):
    if len(Rtmp) >= len(Btmp):
        R=Btmp
        G=Gtmp
        B=Rtmp
    else:
        R=Rtmp
        G=Gtmp
        B=Btmp
else:
    if len(Gtmp) >= len(Btmp):
        R=Rtmp
        G=Btmp
        B=Gtmp
    else:
        R=Rtmp
        G=Gtmp
        B=Btmp



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

        ans += len(B)
        if R[i]>G[j]:
            if (R[i]+diff) in B:
                ans-=1
            if (G[j]-diff) in B:
                ans-=1
            if diff % 2 == 0 and (R[i]-(diff/2)) in B:
                ans-=1
        else:
            if (G[j]+diff) in B:
                ans-=1
            if (R[i]-diff) in B:
                ans-=1
            if diff % 2 == 0 and (G[j]-(diff/2)) in B:
                ans-=1

print(ans)

Submission status: Time Limit Exceeded

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

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)
for i in r:
    for j in g:
        for k in b:
            new=[i,j,k]
            new.sort()
            if abs(new[1]-new[0])!=abs(new[2]-new[1]):
                ans+=1
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

r = {}
b = {}
g = {}
for i in range(len(s)):
    if s[i] == 'R':
        r[i] = s[i]
    elif s[i] == 'B':
        b[i] = s[i]
    else:
        g[i] = s[i]

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

for i in r.keys():
    for j in b.keys():
        for k in g.keys():
            if abs(k -j) == abs(j -i) or abs(k-i) == abs(i-j) or abs(k-j) == abs(k-i) :
                ans -= 1


print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

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

d = {}
for i in range(N):
  d[i] = S[i]

l = list(itertools.combinations(d, 3))
ans = 0
for x in l:
  if (x[1] - x[0] != x[2] - x[1]) and (d[x[0]] != d[x[1]] and d[x[0]] != d[x[2]] and d[x[1]] != d[x[2]]):
    ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = list(input())
L = [[], [], []]
for i in range(N):
  if s[i] == 'R':
    L[0].append(i)
  elif s[i] == 'G':
    L[1].append(i)
  else:
    L[2].append(i)
l = [len(L[i]) for i in range(3)]
ans = l[0] * l[1] * l[2]
bb = []
if l[0] <= l[1] <= l[2]:
    bb = [0, 1, 2]
elif l[0] <= l[2] <= l[1]:
    bb = [0, 2, 1]
elif l[1] <= l[0] <= l[2]:
    bb = [1, 0, 2]
elif l[1] <= l[2] <= l[0]:
    bb = [1, 2, 0]
elif l[2] <= l[0] <= l[1]:
    bb = [2, 0, 1]
elif l[2] <= l[1] <= l[0]:
    bb = [2, 1, 0]
for i in L[bb[0]]:
  for j in L[bb[1]]:
    if (2 * i - j) in L[bb[2]]:
      ans = ans - 1
    if (2 * j - i) in L[bb[2]]:
      ans = ans - 1
    if (i - j) % 2 == 0 and int((i+j)/2) in L[bb[2]]:
      ans = ans - 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
#import copy
#import numpy as np
#import itertools
#import collections
#from collections import deque
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
#from scipy.sparse import csr_matrix

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
#read = sys.stdin.buffer.read

def main():
    # input
    N = int(readline())
    S = input()

    ans = 0
    for i in range(N-2):
        menu = ['R', 'G', 'B']
        si = S[i]
        menu.remove(si)
        for j in range(i+1, N-1):
            if S[j] == si:
                continue
            tmpmenu = menu.copy()
            sj = S[j]
            tmpmenu.remove(sj)
            sk = tmpmenu[0]
            jk_list = [k + 1 for k, v in enumerate(S[j+1:]) if v == sk]
            ans += len([n for n in jk_list if n != j-i])

    print(ans)


if __name__ == "__main__":
    main()

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 - p):
        for r in range(n - p - q):
            a = s[p]
            b = s[p + q]
            c = s[p + q + r]
            if a != b and b != c and c != a and q != r:
                # print(a, b, c, p, q, r)
                counter += 1
print(counter)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=input()
S=input()
N=int(N)
ans=0
ind=[]
tmp={"R":0,"G":0,"B":0}
for i in S:
    tmp[i]+=1
    ind.append(tmp.copy())
#print(ind)
for i in range(0,N-2):
    for j in range(i+1,N-1):
        if S[i]==S[j]:
            continue
        for k in tmp.keys():
            if k!=S[i] and k!=S[j]:
                col=k
                break
        #print(i,j,col)
        if 2*j-i <N and S[j+j-i]==col:
            ans-=1
        ans+=(ind[-1][col]-ind[j][col])
print(ans)

Submission status: Time Limit Exceeded

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

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

ans = 0
for i in range(N-2):
    for k in range(i+2,N):
        if S[i] == S[k]:
            continue
        out = S[i] + S[k]
        moji = None
        for hoge in 'RGB':
            if hoge not in out:
                moji = hoge
                break

        ans += S[i+1:k].count(moji)

        if (k-i)%2 == 0:
            if S[(i+k)//2] == moji:
                ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

S_list = []
for i in S:
    S_list.append(i)
ans = 0
for j in range(N):
    for k in range(j,N):
        for l in range(k,N):
            if not S_list[j] == S_list[k] and not S_list[j] == S_list[l] and not S_list[k] == S_list[l]:
                        if not int(k) - int(j) == int(l) - int(k):
                            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

N = int(input())
S = input()
r_cnt = S.count('R')
g_cnt = S.count('G')
b_cnt = N - (r_cnt + g_cnt)

ans = r_cnt * g_cnt * b_cnt

for i in range(1,(N//2)+1):
	for j in range(N):
		if j + 2*i >= N:
			break
		tmp = [S[j],S[j+i],S[j+2*i]]
		tmp = list(set(tmp))
		if len(tmp) == 3:
			ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#4問目
count = 0
N = int(input())
S = list(input())
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)):
                    count+=1
print(count)

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 s[i] == "R":
        R.append(i)
    elif s[i] == "G":
        G.append(i)
    else:
        B.append(i)

ans = 0
for r in R:
    for g in G:
        for b in B:
            tmp = [r,g,b]
            tmp = list(sorted(tmp))

            if 2*tmp[1] != tmp[0] + tmp[2]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
#from collections import defaultdict
#d = defaultdict(int)
#import fractions
#import math
#import collections
#from collections import deque
#from bisect import bisect_left
N = int(input())
#A = list(map(int,input().split()))
S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
R = []
G = []
B = []
for i in range(N):
    a = S[i]
    if a == "R":
        R.append(i)
    elif a == "G":
        G.append(i)
    else:
        B.append(i)
s = len(R) * len(G) * len(B)
def binary_search(S,key):  #昇順リストSからkeyを探す。
    n = len(S)             #存在したらindex,なかったら-1を返す
    left  = 0
    right = n
    while left < right:
        mid = (left+right) // 2
        if S[mid] == key:
            return mid
        elif S[mid] > key:
            right = mid
        else:
            left = mid + 1
    return -1
for r in R:
    for g in G:
        Min = min(r,g)
        Max = max(r,g)
        c1 = 2 * Min - Max
        if (Min + Max ) % 2 == 0:
            c2 = (Min + Max) / 2
        else:
            c2 = -1
        c3 = 2 * Max - Min
        if binary_search(B,c1) != -1:
            s -= 1
        if binary_search(B,c2) != -1:
            s -= 1
        if binary_search(B,c3) != -1:
            s -= 1
print(s)

Submission status: Time Limit Exceeded

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

ans = 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: continue
            x = s[i] + s[j] + s[k]
            if x == 'RGB' or x == 'RBG' or x == 'BRG' or x == 'BGR' or x == 'GRB' or x == 'GBR': ans += 1
            #c = x.count('R') * x.count('G') * x.count('B')
            #if c == 1: ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n-2):
	for j in range(i+1,n-1):
		if s[i] != s[j]:
			d1 = j-i
			for k in range(j+1,n):
				if (s[i] != s[k]) and (s[j] != s[k]):
					d2 = k-j
					if d1 != d2:
						num += 1

print(num)

Submission status: Time Limit Exceeded

Solution code:
# 文字数を入力
length = int(input())
# テキストを1文字ずつのリストで入力
text = list(input())
# 組み合わせの数のカウント変数
count = 0
# 総当たりで組み合わせを調べる
for i, c1 in zip(range(1, length+1), text[0:]):
    for j, c2 in zip(range(i+1, length+1), text[i:]):
        for k, c3 in zip(range(j+1, length+1), text[j:]):
            if (j - i != k - j) and (c1 != c2 and c1 != c3 and c2 != c3):
                count += 1
print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
s = input()
ans = 0
li_r = []
li_g = []
li_b = []
for i in range(n):
  if s[i] == "R":
    li_r.append(i+1)
  elif s[i] == "G":
    li_g.append(i+1)
  elif s[i] == "B":
    li_b.append(i+1)
for i in li_r:
  for j in li_g:
    for k in li_b:
      li = [i,j,k]
      li.sort()
      a = li[0]
      b = li[1]
      c = li[2]
      if b-a != c-b:
        ans += 1
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)
    elif s[i] == 'G':
        g.append(i)
    else:
        b.append(i)
lr, lg, lb = len(r), len(g), len(b)
ans = lr * lg * lb

if min([lr, lg, lb]) == lr:
    r, b = b, r
if min([lr, lg, lb]) == lg:
    g, b = b, g
for i in r:
    for j in g:
        a = 2 * j - i
        c = 2 * i - j
        if 0 <= a and a <= n-1 and a in b:
            ans -= 1
        if 0 <= c and c <= n-1 and c in b:
            ans -= 1
        if (i + j) % 2 == 0 and (i + j) / 2 in b:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

cnt = len(R)*len(G)*len(B)
        
for i in R:       
    for d in  range(1,1400):
        if i + d in G and i + 2*d in B:
            cnt -= 1
        if i + 2*d in G and i + d in B:
            cnt -= 1
        if i - d in G and i - 2*d in B:
            cnt -= 1
        if i - 2*d in G and i - d in B:
            cnt -= 1
        if i + d in G and i - d in B:
            cnt -= 1
        if i - d in G and i + d in B:
            cnt -= 1

            
print(cnt)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# 6
# RRRGBB
import sys
from collections import Counter

N = int(input())
S = input()
if N < 3:
    print(0)
    sys.exit()
ans = 0
for i in range(N):
    for s in range(1, N):
        if i+s > N-1:
            break
        for t in range(1, N):
            if i+s+t > N-1:
                break
            if s == t:
                continue
            ok = True
            ok &= S[i] != S[i+s]
            ok &= S[i] != S[i+s+t]
            ok &= S[i+s] != S[i+s+t]
            if ok:
                ans += 1
print(ans)