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 math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)

N = readInt()
S = readChar()
rgb = {"R":[],"G":[],"B":[]}
for i in range(N):
	rgb[S[i]].append(i)

c=s=0
for r in rgb["R"]:
	for g in rgb["G"]:
		for b in rgb["B"]:
			a = sorted([r,g,b])
			if a[2]-a[1]!=a[1]-a[0]:
				c+=1


print(c)

Submission status: Time Limit Exceeded

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

m = {'R': 0, 'G': 0, 'B': 0}

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

result = m['R'] * m['G'] * m['B']

for i in range(1, n - 1):
  for j in range(i + 1, n):
    k = 2 * j - i

    if k > n or not (s[i - 1] != s[j - 1] and s[j - 1] != s[k - 1] and s[k - 1] != s[i - 1]):
      continue
    result -= 1

print(result)

Submission status: Time Limit Exceeded

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

m = {'R': [], 'G': [], 'B': []}

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

result = len(m['R']) * len(m['G']) * len(m['B'])

for i in range(1, n - 1):
  for j in range(i + 1, n):
    k = 2 * j - i

    if k > n or not (s[i - 1] != s[j - 1] and s[j - 1] != s[k - 1] and s[k - 1] != s[i - 1]):
      continue
    result -= 1

print(result)

Submission status: Time Limit Exceeded

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

m = {'R': [], 'G': [], 'B': []}

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

result = len(m['R']) * len(m['G']) * len(m['B'])

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

      if j - i == k - j:
        result -= 1

print(result)

Submission status: Time Limit Exceeded

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

if N<=2:
    print(0)
    exit()
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] and (j-i)!=(k-j):
                ans = ans + 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
N = int(input())
S = input()
idxs = range(1, N+1)
ans = 0
for i, j, k in combinations(idxs, 3):
    if S[i-1] != S[j-1] and S[i-1] != S[k-1] and S[j-1] != S[k-1] and (j-i) != (k-j):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
 
ans = 0
for k in range(n):
    for j in range(k + 1):
        for i in range(j + 1):
            if j - i != k - j:
                x = s[k] + s[j] + s[i]
                if x in ['BGR', 'BRG', 'GBR', 'GRB', 'RBG', 'RGB']:
                    ans = ans + 1
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
for k in range(n):
    for j in range(k + 1):
        for i in range(j + 1):
            if j - i != k - j:
                x = s[k] + s[j] + s[i]
                if x in ['BGR', 'BRG', 'GBR', 'GRB', 'RBG', 'RGB']:
                    ans = ans + 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input=sys.stdin.readline
  N=int(input())
  S=input()
  t=0
  for i in range(1,N-1):
    for j in range(i,N):
      for k in range(j,N+1):
        if S[i-1]!=S[j-1] and S[j-1]!=S[k-1] and S[k-1]!=S[i-1] and j-i!=k-j:
          t+=1
  print(t)
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
N=int(input())
S=input()
t=0
for i in range(1,N-1):
  for j in range(i,N):
    for k in range(j,N+1):
      if S[i-1]!=S[j-1] and S[j-1]!=S[k-1] and S[k-1]!=S[i-1] and j-i!=k-j:
        t+=1
print(t)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

c=0
r=[0]*4000
r_tail=0
g=[0]*4000
g_tail=0
b=[0]*4000
b_tail=0
for t in s:
    if t=="R":
        r[r_tail]=c
        r_tail+=1
    elif t=="B":
        b[b_tail]=c
        b_tail+=1
    elif t=="G":
        g[g_tail]=c
        g_tail+=1
    c+=1

if r_tail>b_tail:
    r_tail,b_tail=b_tail,r_tail
    r,b=b,r
elif g_tail>b_tail:
    g_tail,b_tail=b_tail,g_tail
    g,b=b,g


ans=r_tail*b_tail*g_tail
count=0
for i in range(r_tail):
    for j in range(g_tail):
        p=2*g[j]-r[i]
        aa=r[i]+g[j]
        if aa%2==0:
            q=int(aa/2)
        else:
            q=99999999
        o=2*r[i]-g[j]
        if p in b[:b_tail]:
            count+=1
            
        if q in b[:b_tail]:
            count+=1
            
        if o in b[:b_tail]:
            count+=1
            
     
ans=ans-count
print(ans)

Submission status: Time Limit Exceeded

Solution code:
A=[['R','G','B'],['R','B','G'],['B','R','G'],['B','G','R'],['G','R','B'],['G','B','R']]
m=int(input())
N=input()
cnt=0
for i in range(6):
  for j in range(m):
    if N[j]==A[i][0]:
      for k in range(j+1,m):
        if N[k]==A[i][1]:
          for l in range(k+1,m):
            if N[l] == A[i][2]:
              if l-k != k-j:
                cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

c=0
r=[0]*4000
r_tail=0
g=[0]*4000
g_tail=0
b=[0]*4000
b_tail=0
for t in s:
    if t=="R":
        r[r_tail]=c
        r_tail+=1
    elif t=="B":
        b[b_tail]=c
        b_tail+=1
    elif t=="G":
        g[g_tail]=c
        g_tail+=1
    c+=1

ans=r_tail*b_tail*g_tail
count=0
for i in range(r_tail):
    for j in range(g_tail):
        p=2*g[j]-r[i]
        aa=r[i]+g[j]
        if aa%2==0:
            q=int(aa/2)
        else:
            q=99999999
        o=2*r[i]-g[j]
        if p in b[:b_tail]:
            count+=1
            
        if q in b[:b_tail]:
            count+=1
            
        if o in b[:b_tail]:
            count+=1
            
     
ans=ans-count
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=0
tmp=0
for k in range(N):
  for j in range(k):
    i=int(2*j-k)
    if i>=0:
      if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
        tmp+=1
ans=S.count("R")*S.count("G")*S.count("B")-tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=0
tmp=0
for k in range(2,N):
  for j in range(1,k):
    i=int(2*j-k)
    if i>=0:
      if S[i]!=S[j] and S[i]!=S[k] and S[j]!=S[k]:
        tmp+=1
ans=S.count("R")*S.count("G")*S.count("B")-tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
s = str(stdin.readline().rstrip())
#l = list(map(int, stdin.readline().rstrip().split()))
#A, B, C = map(int, stdin.readline().rstrip().split())
#S = [list(map(int, stdin.readline().rstrip().split())) for _ in range(h)]#hの定義を忘れずに

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

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

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

counter = 0

for i, s1 in enumerate(S):
    #rgb = [f]
    for j, s2 in enumerate(S[i+1:]):
        if s1 == s2:
            continue
        else:
            d = j+1
            #print(d)
            #print(S[i+j+2:])
            for k, s3 in enumerate(S[i+j+2:]):
                if s3 != s1 and s3 != s2 and d != k+1:
                    counter += 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    s = input()

    ans = 1
    for c in 'RGB':
        ans *= s.count(c)

    for j in range(N):
        for i in range(j):
            k = (j - i) + j
            if k >= N: continue
            if len({s[x] for x in (i, j, k)}) == 3:
                ans -= 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=0
RGB_dict={'R':0,'G':0,'B':0}
for s in S:
  RGB_dict[s]+=1
ans=RGB_dict['R']*RGB_dict['G']*RGB_dict['B']

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
ans=0
for j in range(1,N-1):
  s1=S[j]
  for i in range(0,j):
    s2=S[i]
    if s1==s2: continue
    jmi=j-i
    for k in range(j+1,N):
      if k-j==jmi: continue
      s3=S[k]
      if s1!=s3 and s2!=s3:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    n = 0
    for i in range(len(S)):
        for j in range(i+1, len(S)):
            for k in range(j+1, len(S)):
                if not(j-i == k-j):
                    if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
                        n =n+1
    print(n)
main()

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(N):
    for j in range(i,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()
result = 0

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

result = r * g * b
count = 0

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

print(result-count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = s.count("R") * s.count("G") * s.count("B")

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = s.count("R") * s.count("G") * s.count("B")

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

from collections import defaultdict
d = defaultdict(int)

for i in range(N):
    s = S[i]
    d[s] += 1
cnt = 0
for i in range(N-2):
    for j in range(i,N-1):
        k = 2*j - i
        if k <= N-1 and (S[i] != S[j] and S[j] != S[k] and S[k] != S[i]):
            cnt += 1
if d["R"] and d["G"] and d["B"]:
    ans = d["R"] * d["G"] * d["B"] - cnt
    print(ans)
else:
    print(0)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 0
for i in range(n-2):
  for 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
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

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

        if x == y:
            return

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

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

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

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

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

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

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

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

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

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

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

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

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

    return arr

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

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

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

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret


# ここから書き始める
n = int(input())
s = input()
c = []
cntr = 0
cntg = 0
cntb = 0
for i in s:
    if i == "R":
        cntr += 1
        if cntg > 0:
            c.append(["G", cntg])
            cntg = 0
        elif cntb > 0:
            c.append(["B", cntb])
            cntb = 0
    elif i == "G":
        cntg += 1
        if cntr > 0:
            c.append(["R", cntr])
            cntr = 0
        elif cntb > 0:
            c.append(["B", cntb])
            cntb = 0
    else:
        cntb += 1
        if cntr > 0:
            c.append(["R", cntr])
            cntr = 0
        elif cntg > 0:
            c.append(["G", cntg])
            cntg = 0
    # print("c =", c, end="")
    # print("cntr = %d cntg = %d cntb = %d" % (cntr, cntg, cntb))
if cntr > 0:
    c.append(["R", cntr])
elif cntg > 0:
    c.append(["G", cntg])
elif cntb > 0:
    c.append(["B", cntb])
m = len(c)
ans = 0
# print("c =", c)
for i in range(m - 2):
    cnt1 = 0
    cnt2 = 0
    if c[i][0] == "R":
        for j in range(i + 1, m):
            if c[j][0] == "G":
                cnt1 += c[j][1]
            elif c[j][0] == "B":
                cnt2 += c[j][1]
    elif c[i][0] == "G":
        for j in range(i + 1, m):
            if c[j][0] == "R":
                cnt1 += c[j][1]
            elif c[j][0] == "B":
                cnt2 += c[j][1]
    else:
        for j in range(i + 1, m):
            if c[j][0] == "R":
                cnt1 += c[j][1]
            elif c[j][0] == "G":
                cnt2 += c[j][1]
    ans += c[i][1] * cnt1 * cnt2
# print(ans)
# ans2 = 0
for i in range(n - 2):
    for j in range(i + 1, n):
        k = j + (j - i)
        if k >= n:
            break
        if sorted([s[i], s[j], s[k]]) == ["B", "G", "R"]:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
s=input()

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

for i in range(0,n):
    if s[i]=='R':
        r.append(i)

    elif s[i]=='G':
        g.append(i)

    elif s[i]=='B':
        b.append(i)

s3=0
rl=len(r)
gl=len(g)
bl=len(b)

for i in range(0,n-2):
    for j in range(i,n-1):
        k=2*j-i
        if k<=n-1:
            
            if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                s3+=1
s2=rl*gl*bl-s3
print(s2)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
s=input()

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

for i in range(0,n):
    if s[i]=='R':
        r.append(i)

    elif s[i]=='G':
        g.append(i)

    elif s[i]=='B':
        b.append(i)

s=0
rl=len(r)
gl=len(g)
bl=len(b)
#ml=max(rl,gl,bl)
#if  ml==rl:
#    d1=r
#    d2=g
#    d3=b
#elif ml==gl:
#    d1=g
#    d2=r
#    d3=b
#else:
#    d1=b
#    d2=g
#    d3=r

for i1 in r:
    for i2 in g:
        if (min(i1,i2)-np.abs(i1-i2)) in b:
            s+=1
        if (max(i1,i2)+np.abs(i1-i2)) in b:
            s+=1
        if (i1+i2)/2.0 in b:
            s+=1

s2=rl*gl*bl-s
print(s2)

Submission status: Time Limit Exceeded

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

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


ans = r * g * b

for i in range(n):
    for j in range(i + 1, n):
        if 2 * j - i > n - 1:
            continue
        if s[i] == s[j] or s[i] == s[2 * j - i] or s[j] == s[2 * j - i]:
            pass
        else:
            ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = list(input())
    count = {"G": 0, "R":0, "B":0}
    for i in s:
        count[i] += 1
    ans = count["G"]*count["R"]*count["B"]
    for j in range(n):
        for i in range(1, n//2+1):
            if j+2*i > n-1:
                continue
            if len(set([s[j], s[j+i], s[j+2*i]])) == 3:
                ans -= 1
    print(ans)
    
    

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
s=input()

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

for i in range(0,n):
    if s[i]=='R':
        r.append(i)

    elif s[i]=='G':
        g.append(i)

    elif s[i]=='B':
        b.append(i)

s=0
rl=len(r)
gl=len(g)
bl=len(b)
ml=max(rl,gl,bl)
if  ml==rl:
    d1=r
    d2=g
    d3=b
elif ml==gl:
    d1=g
    d2=r
    d3=b
else:
    d1=b
    d2=g
    d3=r

for i1 in d2:
    for i2 in d3:
        if (min(i1,i2)-np.abs(i1-i2)) in d1:
            s+=1
        if (max(i1,i2)+np.abs(i1-i2)) in d1:
            s+=1
        if (i1+i2)/2.0 in d1:
            s+=1

s2=rl*gl*bl-s
print(s2)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
s=input()

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

for i in range(0,n):
    if s[i]=='R':
        r.append(i)

    elif s[i]=='G':
        g.append(i)

    elif s[i]=='B':
        b.append(i)

s=0
for i1 in r:
    for i2 in g:
        for i3 in b:
            if np.abs(i1-i2)!=np.abs(i1-i3) and np.abs(i1-i2)!=np.abs(i2-i3) and np.abs(i1-i3)!=np.abs(i2-i3):
                s+=1

print(s)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
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]:
            continue
        for k in range(j+1,n):
            if 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:
# coding: utf-8
# Your code here!
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[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_list = list(str(input()))
r = 0
g = 0
b = 0

for i in range(n):
    if s_list[i] == 'R':
        r += 1
    elif s_list[i] == 'G':
        g += 1
    else:
        b += 1

ans = r * g * b

for d in range(1, n):
    for i in range(n):
        if i + 2 * d < n and \
            s_list[i] != s_list[i + d] and \
            s_list[i] != s_list[i + 2 * d] and \
            s_list[i + d] != s_list[i + 2 * d]:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
    for j in range(i + 1, n):
        if s_list[i] == s_list[j]:
            continue
        else:
            for k in range(j + 1, n):
                if s_list[i] != s_list[k] and s_list[j] != s_list[k] and j - i != k - j:
                    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
r = []
g = []
b = []
cnt = 1
sum = 0

for i in input():
    if i == 'R':
        r.append(cnt)
    elif i == 'G':
        g.append(cnt)
    elif i == 'B':
        b.append(cnt)
    cnt+=1

for i in r:
    for j in g:
        for k in b:
            li = [i,j,k]
            li.sort()
            # print(li)
            tmo = li[1] - li[0]
            tmp = li[2] - li[1]
            if tmo != tmp:
                sum += 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
rgb = {}
rgb["R"] = []
rgb["G"] = []
rgb["B"] = []
cnt = 1
sum = 0
for i in input():
    rgb[i].append(cnt)
    cnt+=1


for i in rgb["R"]:
    for j in rgb["G"]:
        for k in rgb["B"]:
            li = [i,j,k]
            li.sort()
            # print(li)
            tmo = li[1] - li[0]
            tmp = li[2] - li[1]
            if tmo != tmp:
                sum += 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = 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 ri in r:
    for gi in g:
        for bi in b:
            arr = [ri, gi, bi]
            arr.sort()
            if arr[0] + arr[2] == 2 * arr[1]:
                pass
            else:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = 0
g = 0
b = 0
for letter in s:
    if letter == "R":
        r +=1
    elif letter == "G":
        g +=1
    elif letter == "B":
        b +=1
total = r * g * b
subs = 0
for i in range(n):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k <= n - 1:
            if s[i] == s[j]:
                continue
            elif s[j] == s[k]:
                continue
            elif s[k] == s[i]:
                continue
            else:
                subs += 1
        else:
            continue
ans = total - subs
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
total = r * g * b
subs = 0
for i in range(n):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k <= n - 1:
            if s[i] == s[j]:
                continue
            elif s[j] == s[k]:
                continue
            elif s[k] == s[i]:
                continue
            else:
                subs += 1
        else:
            continue
ans = total - subs
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = 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 ri in r:
    for gi in g:
        for bi in b:
            if abs(ri - gi) == abs(gi - bi) or abs(ri - bi) == abs(gi - bi) or abs(ri - bi) == abs(ri - gi):
                pass
            else:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
total = r * g * b
subs = 0
for i in range(n):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k <= n - 1:
            if len(set([s[i], s[j], s[k]])) == 3:
                subs += 1
        else:
            break
ans = total - subs
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = s.count("R")
g = s.count("G")
b = s.count("B")
total = r * g * b
subs = 0
for i in range(n):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k <= n - 1:
            if len(set([s[i], s[j], s[k]])) == 3:
                subs += 1
        else:
            continue
ans = total - subs
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)
    elif s[i] == "B":
        b.append(i)
total = len(r) * len(g) * len(b)
subs = 0
for l in range(len(r)):
    for m in range(len(g)):
        for n in range(len(b)):
            nums = [r[l], g[m], b[n]]
            nums.sort()
            if nums[2] - nums[1] == nums[1] - nums[0]:
                subs += 1
            else:
                continue
ans = total - subs
print(ans)

Submission status: Time Limit Exceeded

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

R = []
G = []
B = []
i = 1
for c in S:
    if c == 'R':
        R.append(i)
    elif c == 'G':
        G.append(i)
    elif c == 'B':
        B.append(i)
    i += 1

ans = 0

for r in R:
    for g in G:
        for b in B:
            if abs(r-g) != abs(r-b) and abs(r-b) != abs(g-b) and abs(r-g) != abs(g-b):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

R = [i for i, x in enumerate(S) if x == "R"]
G = [i for i, x in enumerate(S) if x == "G"]
B = [i for i, x in enumerate(S) if x == "B"]

RGB = list(itertools.product(R,G, B))

result = 0
for item in RGB:
    s_item = sorted(item)
    if s_item[2] - s_item[1] != s_item[1] - s_item[0]:
        result += 1

print(result)

Submission status: Time Limit Exceeded

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

R = [i for i, x in enumerate(S) if x == "R"]
G = [i for i, x in enumerate(S) if x == "G"]
B = [i for i, x in enumerate(S) if x == "B"]

result = 0
for i in R:
    for j in G:
        for k in B:
            item = sorted([i, j, k])
            if item[2] - item[1] != item[1] - item[0]:
                result += 1
print(result)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys
input = sys.stdin.readline

def Solve(N:int,S:str) -> int:
    total = Counter(S)
    counter1 = {"R":0,"G":0,"B":0}
    ans = 0

    for i in range(N-2):
        RGB = {"R","G","B"} - {S[i]}
        counter1[S[i]]+=1
        counter2 = {"R":0,"G":0,"B":0}
        for j in range(i+1,N-1):
            counter2[S[j]]+=1
            tmp = list(RGB - {S[j]})
            if len(tmp)==1:
                target = tmp[0]
                ans+=total[target]-counter1[target]-counter2[target]
                if((j - i)+ j <= N -1 and S[(j - i)+ j] == target):
                    ans-=1
    return ans

def main():
    N = int(input())
    S = input()
    print(Solve(N,S))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
S = list(input())
total = Counter(S)
counter1 = {"R":0,"G":0,"B":0}
ans = 0


for i in range(N-2):
    RGB = {"R","G","B"} - {S[i]}
    counter1[S[i]]+=1
    counter2 = {"R":0,"G":0,"B":0}
    for j in range(i+1,N-1):
        counter2[S[j]]+=1
        tmp = list(RGB - {S[j]})
        if len(tmp)==1:
            target = tmp[0]
            ans+=total[target]-counter1[target]-counter2[target]
            if((j - i)+ j <= N -1 and S[(j - i)+ j] == target):
                ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N = int(input())
S = list(input())
dic = Counter(S)
dic2 = {"R":0,"G":0,"B":0}
ans = 0
for i in range(N-2):
    RGB = {"R","G","B"} - {S[i]}
    dic2[S[i]]+=1
    dic3 = {"R":0,"G":0,"B":0}
    for j in range(i+1,N-1):
        dic3[S[j]]+=1
        if len(RGB - {S[j]})!=1:
            continue
        else:
            target = list(RGB - {S[j]})[0]
            ans+=dic[target]-dic2[target]-dic3[target]
            if((j - i)+ j <= N -1):
                if(S[(j - i)+ j] == target):
                    ans-=1
print(ans)