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:
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

N = INT()
S = input()

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
def solve(rr,gg,bb):
    d1,d2=defaultdict(list),defaultdict(list)
    for i in range(len(gg)):
        ii=gg[i]
        for j in range(len(rr)):
            jj=rr[j]
            if ii>jj:
                d1[ii].append(ii-jj)
    for i in range(len(gg)):
        ii=gg[i]
        for j in range(len(bb)):
            jj=bb[j]
            if ii<jj:
                d2[ii].append(jj-ii)
    s=0
    #print(d1,d2)
    for x in d1:
        for y in d1[x]:
            for z in d2[x]:
                if y!=z:
                    s+=1
    return s
        
n=int(input())
s=input()
l=list(s)
rr,gg,bb=[],[],[]
for i in range(len(s)):
    ii=s[i]
    if ii=='R':rr.append(i)
    if ii=="G":gg.append(i)
    if ii=="B":bb.append(i)
#print(rr,gg,bb)
print(solve(rr,gg,bb)+solve(rr,bb,gg)+solve(bb,rr,gg)+solve(bb,gg,rr)+solve(gg,rr,bb)+solve(gg,bb,rr))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
count=0
r=[]
g=[]
b=[]
for i in range(N):
	if S[i]=="R":
		r.append(i+1)
	if S[i]=="G":
		g.append(i+1)
	if S[i]=="B":
		b.append(i+1)
for x in r:
	for y in g:
		for z in b:
			L=[x,y,z]
			#print(L)
			L=sorted(L)
			if (L[1]-L[0])!=(L[2]-L[1]):
				count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env pypy3
# n,m = map(int,sys.stdin.readline().split())
# a = list(map(int,sys.stdin.readline().split()))
# a = [sys.stdin.readline() for s in range(n)]
# s = sys.stdin.readline().rstrip()
# n = int(sys.stdin.readline())
# d = collections.defaultdict(list)
# import math
# from functools import reduce
import sys
sys.setrecursionlimit(15000)

n = int(sys.stdin.readline())
s = sys.stdin.readline().rstrip()
d = {"R":[],"G":[],"B":[]}
for i in range(n):
    d[s[i]].append(i)
s = 0
for a in d["R"]:
    for b in d["G"]:
        for c in d["B"]:
            if abs(a-b) != abs(b-c) and abs(a-c) != abs(c-b) and abs(b-a) != abs(a-c):
                s += 1
print(s)

Submission status: Time Limit Exceeded

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

s = 0
for a in range(N):
    s1 = S[a]
    for b in range(a+1, N):
        s2 = S[b]
        if s1 == s2:
            continue
        n = S[b+1:]
        pat = ['R', 'G', 'B']
        pat.remove(s1)
        pat.remove(s2)
        count = n.count(pat[0])
        d = b - a
        if len(n) > d-1 and n[d-1] == pat[0]:
            count -= 1
        s += count

print(s)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    s=input()
    R=[]
    G=[]
    B=[]
    for i in range(1,n+1):
        if s[i-1]=='R':R.append(i)
        elif s[i-1]=='G':G.append(i)
        else:B.append(i)
    sum=0
    for r in R:
        for g in G:
            for b in B:
                n_list=[r,g,b]
                n_list.sort()
                if n_list[1]-n_list[0]!=n_list[2]-n_list[1]:
                    sum+=1
    print(sum)

if __name__=='__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
count=0
i=0
j=0
k=0
while i<n:
    j=i+1
    while j<n:
        k=j+1
        while k<n:
            if (s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k] and j-i!=k-j):
                count+=1
            else:
                pass
            k+=1
        j+=1
    i+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

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

ans = 0

for i in range(N - 2):
  for j in range(N - i - 2):
    for k in range(N - i - j - 2):

      if S[i] != S[i + j + 1] and S[i + j + 1] != S[i + j + k + 2] and S[i + j + k + 2] != S[i] and j != k:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(0, N-2):
  for j in range(i+1, N-1):
    if S[i] != S[j]:
      if S[i] == "R": 
        if S[j] == "G":
          ans = ans + S.count("B", j)
          try:
            if S[j+(j-i)] == "B":
              ans = ans -1
          except:
            None
        if S[j] == "B":
          ans = ans + S.count("G", j)
          try:
            if S[j+(j-i)] == "G":
              ans = ans -1
          except:
            None
      if S[i] == "G": 
        if S[j] == "B":
          ans = ans + S.count("R", j)
          try:
            if S[j+(j-i)] == "R":
              ans = ans -1
          except:
            None
        if S[j] == "R":
          ans = ans + S.count("B", j)
          try:
            if S[j+(j-i)] == "B":
              ans = ans -1
          except:
            None
      if S[i] == "B": 
        if S[j] == "R":
          ans = ans + S.count("G", j)
          try:
            if S[j+(j-i)] == "G":
              ans = ans -1
          except:
            None
        if S[j] == "G":
          ans = ans + S.count("R", j)
          try:
            if S[j+(j-i)] == "R":
              ans = ans -1
          except:
            None

print(ans)

Submission status: Time Limit Exceeded

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

r_list = [i for i, x in enumerate(s) if x == 'R']
g_list = [i for i, x in enumerate(s) if x == 'G']
b_list = [i for i, x in enumerate(s) if x == 'B']

result = 0
for r_index in r_list:
    for g_index in g_list:
        for b_index in b_list:
            combination = sorted([r_index, g_index, b_index])
            if combination[2]-combination[1] != combination[1]-combination[0]:
                result += 1
            # print(combination)

print(result)

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(0, n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            if not s[i] == s[j]:
                if not (j-i) == (k-j):
                    if not s[i] == s[k]:
                        if not s[j] == s[k]:
                            cnt += 1
print(cnt)

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

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# 整数を一つ入力
N = int(input())

# 文字列を一つ入力
S = input()

res = 0

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

print(res)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = input()

l_r = [i for i, s in enumerate(S) if s=='R']
l_g = [i for i, s in enumerate(S) if s=='G']
l_b = [i for i, s in enumerate(S) if s=='B']

ans = 0
for r in l_r:
  for g in l_g:
    for b in l_b:
      li = np.sort(np.array([r, g, b]))
      if abs(li[0]-li[1]) != abs(li[1]-li[2]):
        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)
res = 0
for i in r:
  for j in b:
    flg = 0
    if (i + j) / 2 in g:
      flg += 1
    if 2 * i - j in g:
      flg += 1
    if 2 * j - i in g:
      flg += 1
    res += len(g) - flg
print(res)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import itertools


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

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

                ans += 1

    print(ans)
    return 0

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env pypy3
# n,m = map(int,sys.stdin.readline().split())
# a = list(map(int,sys.stdin.readline().split()))
# a = [sys.stdin.readline() for s in range(n)]
# s = sys.stdin.readline().rstrip()
# n = int(sys.stdin.readline())
# d = collections.defaultdict(list)
# import math
# from functools import reduce
import sys
sys.setrecursionlimit(15000)

n = int(sys.stdin.readline())
s = sys.stdin.readline().rstrip()
d = {"R":[],"G":[],"B":[]}
for i in range(n):
    d[s[i]].append(i)
s = 0
for a in d["R"]:
    for b in d["G"]:
        for c in d["B"]:
            if abs(a-b) != abs(b-c) and abs(a-c) != abs(c-b) and abs(b-a) != abs(a-c):
                s += 1
print(s)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

def cal(l1,l2,l3):
    cnt=0
    for _l1 in range(len(l1)):
        for _l2 in range(len(l2)):
            if l1[_l1]<l2[_l2]:
                for _l3 in range(len(l3)):
                    if l2[_l2]<l3[_l3]:
                        if l3[_l3]-l2[_l2]!=l2[_l2]-l1[_l1]:
                            cnt+=1
    return cnt
        
count=0
count+=cal(r,g,b)
count+=cal(r,b,g)
count+=cal(g,r,b)
count+=cal(g,b,r)
count+=cal(b,r,g)
count+=cal(b,g,r)
print(count)

Submission status: Time Limit Exceeded

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

L = ['R', 'G', 'B']
accum = [[0] * N for i in range(3)]
for i in range(3):
    l = L[i]
    if S[0] == l:
        accum[i][0] = 1
    for j in range(1, N):
        accum[i][j] = accum[i][j - 1] + (1 if S[j] == l else 0)

ans = 0
for j in range(1, N - 1):
    for i in range(j):
        if S[i] == S[j]:
            continue
        for k in range(3):
            if L[k] != S[i] and L[k] != S[j]:
                lk = L[k]
                ik = k
        ans += accum[ik][N - 1] - accum[ik][j]
        if 2 * (j + 1) - (i + 1) <= N and S[2 * (j + 1) - (i + 1) - 1] == lk:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

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

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

     print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np

def solve(N: int, S: str):
    r = []
    g = []
    b = []

    for i, x in enumerate(S):
        if x ==  "R":r.append(i)
        elif x =="G":g.append(i)
        elif x =="B":b.append(i)

    r = np.array(r)
    g = np.array(g)
    b = np.array(b)


    def rgb(r, g, b):
        sr = set(r)
        sg = set(g)
        sb = set(b)
        ans = 0
        for r_ in r:
            for g_ in g[g>r_]:
                if (g_ + g_ - r_) in sb:
                    ans -= 1
                ans += len(b[b>g_])
        return ans

    k = rgb(r,g,b) + rgb(r,b,g) + rgb(b,g,r) + rgb(b,r,g) + rgb(g,r,b) + rgb(g,b,r)
    print(k)


    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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


print(cnt)

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)
		M=max(r,g)
		m=min(r,g)
		if (2*M-m) in B:
			ans-=1
		if (2*m-M) in B:
			ans-=1
		if (M+m)%2==0:
			if (M+m)//2 in B:
				ans-=1
print(ans)

Submission status: Time Limit Exceeded

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

R, G, B = [], [], []
for i, c in enumerate(s):
  if c == 'R': R.append(i)
  elif c == 'G': G.append(i)
  else: B.append(i)

ans = 0
for r in R:
  for g in G:
    for b in B:
      if abs(r-g) != abs(g-b) and abs(r-b) != abs(g-b) and abs(r-g) != abs(r-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+1, N-1):
        if S[i] == S[j]:
            continue
        else:
            for k in range(j+1, N):
                if k == (2*j - i):
                    continue
                else:
                    if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                        res += 1
print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# import itertools


def main():
    n = int(input())
    s = input()
    # comb = itertools.combinations(range(n), 3)
    ans = 0
    for a in range(n):
        for b in range(a+1, n):
            for c in range(b+1, n):
                if b - a == c - b:
                    continue
                if len(set((s[a], s[b], s[c]))) == 3:
                    ans += 1
    return ans


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

Submission status: Time Limit Exceeded

Solution code:


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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    count_r = 0
    count_g = 0
    count_b = 0
    N = int(input())
    S = input()
    total = 0
    for ind, ch in enumerate(S):
        if ch == 'R':
            count_r += 1
        elif ch == 'G':
            count_g += 1
        else:
            count_b += 1
    for i in range(N):
        for j in range(i + 1, N):
            if S[i] != S[j]:
                r, g = S[i], S[j]
                other = 'B'
                if 'G' != r and 'G' != g:
                    other = 'G'
                if 'R' != r and 'R' != g:
                    other = 'R'
                k = j - i + j
                if k < N and S[k] == other:
                    total -= 1
    print(total + count_r*count_b*count_g)

Submission status: Time Limit Exceeded

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

R = [0]*(n+10)
G = [0]*(n+10)
B = [0]*(n+10)


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

ans = 0

for i in range(n):
    if s[i] == "R":
        for j in range(i+1,n):
            if  s[j] == "G":
                ans += sum(B[j:])
                if j+j-i > n:
                    continue
                if B[j+j-i] == 1:
                    ans -= 1
            if s[j] == "B":
                ans += sum(G[j:])
                if j+j-i > n:
                    continue
                if G[j+j-i] == 1:
                    ans -= 1               

    if s[i] == "B":
        for j in range(i+1,n):
            if  s[j] == "R":
                ans += sum(G[j:])
                if j+j-i > n:
                    continue
                if G[j+j-i] == 1:
                    ans -= 1
            if s[j] == "G":
                ans += sum(R[j:])
                if j+j-i > n:
                    continue
                if R[j+j-i] == 1:
                    ans -= 1

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

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]
            c = x.count('R') * x.count('G') * x.count('B')
            if c == 1: ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

kouho = list(itertools.product(R, G, B))
ans = 0
for i in range(len(kouho)):
  temp = [kouho[i][0], kouho[i][1], kouho[i][2]]
  if abs(temp[1] - temp[0]) != abs(temp[2] - temp[1]) and abs(temp[2] - temp[0]) != abs(temp[1] - temp[0]) and abs(temp[2] - temp[0]) != abs(temp[2] - temp[1]):
    ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    # N = 39
    # S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
    
    # 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(len(S)):
            
            if i >= j:
                continue
            

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

                # if (j - i) == (k - j):
                #     print('---------------------------')
                #     print('2:', i, j, k)
                #     print('j - i, k - j:', j - i, k - j)
    
    # print(C, out)
    # print(R)
    # print(G)
    # print(B)
    # print(len(R) * len(G) * len(B))
    print(out)

    
main()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

ans = 0

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

print(ans)

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
r_pos = []
g_pos = []
b_pos = []
for i in range(n):
    st = s[i]
    if st == "R":
        r_pos.append(i)

    if st == "G":
        g_pos.append(i)

    if st == "B":
        b_pos.append(i)

#print(r_pos, g_pos, b_pos)
ans = 0
for r in r_pos:
    for g in g_pos:
        for b in b_pos:
            color = [r, g, b]
            color.sort()
            x = color[0]
            y = color[1]
            z = color[2]
            if z - y != y - x:
                ans += 1
print(ans)

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):
        ta = s[ia]
        for ib in range(ia+1, n-1):
            tb = s[ib]
            t = (ta, tb)
            sa = ib - ia
            if ta == tb:
                pass
            else:
                cnt = 1
                ts = s[ib+1:]
                for ets in ts:
                    if cnt == sa:
                        pass
                    elif not ets in t:
                        r += 1
                    cnt += 1
    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
str=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 2*j==i+k:
        continue
      if str[i]==str[j] or str[j]==str[k] or str[i]==str[k]:
        continue
      count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
cnt = 0
cnt_1 = 0
for i in range(N) :
    for j in range(i+1,N) :
        for k in range(j+1,N) :
            cnt_1 += 1
            if ((S[i] == S[j]) or (S[i] == S[k]) or (S[j] == S[k])) or (k == 2*j-i) :
                cnt += 1
print(cnt_1 - cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

N = int(input())
S = input().strip()
l = {'R', 'G', 'B'}

ans = 0
for i, x in enumerate(S):
    for j, y in enumerate(S[i + 1:]):
        if x == y:
            continue
        else:
            j += i + 1
            s = (l - {x, y}).pop()
            k = [n + j + 1 for n, v in enumerate(S[j + 1:]) if v == s]
            if j + (j - i) in k:
                ans += len(k) - 1
            else:
                ans += len(k)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

sum = 0
for i in range(N):
    for j in range(i+1, N):
        if S[i] == S[j]:
            continue

        RGB = ['R','G','B']
        RGB.remove(S[j])
        RGB.remove(S[i])
        c = Counter(S[(j+1):])
        sum += c[RGB[0]]

        # S[k]
        k = 2 * j - i
        if k < len(S):
            if S[k] == RGB[0]:
                sum -= 1
print(sum)

Submission status: Time Limit Exceeded

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

#RL = [0]*(N+1)
#GL = RL.copy
BL = [0]*(N+1)
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)
        BL[i+1] = 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])

        index = (min(R[i], G[j])-tmp_abs)
        if index > 0 and BL[index] == 1:
            ans -= 1

        index = (max(R[i], G[j])+tmp_abs)
        if index <= N and BL[index] == 1:
            ans -= 1

        if tmp_abs % 2 == 0 and BL[max(R[i], G[j])-(tmp_abs//2)] == 1:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
IS = lambda: sys.stdin.readline().rstrip()
II = lambda: int(IS())
MII = lambda: list(map(int, IS().split()))

def main():
    n = II()
    s = IS()
    cnt = 0
    rgb = {'R': [], 'G': [], 'B': []}
    for i in range(n):
        rgb[s[i]].append(i)
    for a in rgb['R']:
        for b in rgb['G']:
            for c in rgb['B']:
                i,j,k = sorted([a,b,c])
                if j-i!=k-j: cnt+=1
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# 入力は二行でそれぞれ[数値]

input_nums = int(input())
input_line = input()

result = 0

for k in range(input_nums):
    for j in range(k):
        for i in range(j):
            if(j-i != k-j):
                if(input_line[i] != input_line[j] and input_line[i] != input_line[k] and input_line[j] != input_line[k]):
                    result = result + 1

print(result)