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 numpy as np
n=int(input())
s=list(input())
sr=[0]*n
sg=[0]*n
sb=[0]*n
for ii in range(n):
    if s[ii]=="R":
        sr[ii]=1
    if s[ii]=="G":
        sg[ii]=1
    if s[ii]=="B":
        sb[ii]=1

r=0
"""
print(sr)
print(sg)
print(sb)
"""
gb=[0]*n
bg=[0]*n
rb=[0]*n
br=[0]*n
rg=[0]*n
gr=[0]*n
for jj in np.arange(1,n-1):
    if sg[jj]>0:
        gb[jj]=sum(sb[jj+1:n])
        gr[jj]=sum(sr[jj+1:n])
    if sb[jj]>0:
        bg[jj]=sum(sg[jj+1:n])
        br[jj]=sum(sr[jj+1:n])
    if sr[jj]>0:
        rb[jj]=sum(sb[jj+1:n])
        rg[jj]=sum(sg[jj+1:n])

for ii in np.arange(1,n-1):
  for jj in np.arange(ii+1,n):
    if s[ii-1]!=s[jj-1] and jj<n:
        kk=2*jj-ii-1
        if (s[ii-1]=="R" and s[jj-1]=="G") or (s[jj-1]=="R" and s[ii-1]=="G"):
            if s[ii-1]=="R":
                r+=gb[jj-1]
            if s[ii-1]=="G":
                r+=rb[jj-1]
            if kk<n:
                r-=sb[kk]
        if (s[ii-1]=="R" and s[jj-1]=="B") or (s[jj-1]=="R" and s[ii-1]=="B"):
            if s[ii-1]=="R":
                r+=bg[jj-1]
            if s[ii-1]=="B":
                r+=rg[jj-1]
            if kk<n:
                r-=sg[kk]
        if (s[ii-1]=="B" and s[jj-1]=="G") or (s[jj-1]=="B" and s[ii-1]=="G"):
            if s[ii-1]=="B":
                r+=gr[jj-1]
            if s[ii-1]=="G":
                r+=br[jj-1]
            if kk<n:
                r-=sr[kk]
        """
      for kk in np.arange(jj+1,n+1):
        #print(ii,jj,kk)
        if jj-ii!=kk-jj:
          if s[jj-1]!=s[kk-1] and s[ii-1]!=s[kk-1]:
            r+=1
        """
print(r)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
RL = []
GL = []
BL = []
for i in range(1, n + 1):
    word = s[i - 1]
    if word == "R":
        RL.append(i)
    elif word == "G":
        GL.append(i)
    else:
        BL.append(i)
ans = 0
for r in RL:
    for g in GL:
        for b in BL:
            delta1 = abs(r - g)
            delta2 = abs(g - b)
            delta3 = abs(b - r)
            if delta1 == delta2:
                continue
            if delta2 == delta3:
                continue
            if delta3 == delta1:
                continue
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

r_indexes = [i for i, x in enumerate(S) if x == 'R']
g_indexes = [i for i, x in enumerate(S) if x == 'G']
b_indexes = [i for i, x in enumerate(S) if x == 'B']

# print(r_indexes)

count=0
for r in r_indexes:
    for g in g_indexes:
        for b in b_indexes:
            sort = sorted([r,g,b])
            if sort[2]-sort[1] != sort[1]-sort[0]:
                count+=1
print(count)

# for i in range(N):
#     s_i = S[i]
#
#     for j in range(i+1, N):
#         s_j = S[j]
#         if s_i == s_j:
#             continue
#
#         for k in range(j+1, N):
#             s_k = S[k]
#
#             if s_i!=s_j and s_i!=s_k and s_j!=s_k and j-i!=k-j:
#                 count+=1

# print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 0

for x in range(n):
  for y in range(n):
    if not x < y:
      continue
    if s[x] == s[y]:
      continue
    for z in range(n):
      if not y < z:
        continue

      if not x < y < z:
        # print("SKIP: ", x, y, z)
        continue

      if y-x == z-y:
        # print("SKIP: ", x, y, z)
        continue

      if not ((s[x] != s[y]) and (s[x] != s[z]) and (s[y] != s[z])):
        # print("SKIP: ", x, y, z)
        continue

      ans += 1
      # print(x, y, z)
      # print(s[x], s[y], s[z])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
def findb(ind,checker):
    global count
    b = S[ind:].count("B")
    if ind + checker < N:
        if S[ind+checker] == "B":
            b -= 1
    if b > 0:
        count += b
def findg(ind,checker):
    global count
    g = S[ind:].count("G")
    if ind + checker < N:
        if S[ind+checker] == "G":
            g -= 1
    if g > 0:
        count += g
def findr(ind,checker):
    global count
    r = S[ind:].count("R")
    if ind + checker < N:
        if S[ind+checker] == "R":
            r -= 1
    if r > 0:
        count += r
for i in range(N):
    if S[i] == "R":
        for j in [x for x, y in enumerate(S[i:]) if y == 'G']:
            findb(i+j,j)
        for j in [x for x, y in enumerate(S[i:]) if y == 'B']:
            findg(i+j,j)
    elif S[i] == "G":
        for j in [x for x, y in enumerate(S[i:]) if y == 'B']:
            findr(i+j,j)
        for j in [x for x, y in enumerate(S[i:]) if y == 'R']:
            findb(i+j,j)
    else:
        for j in [x for x, y in enumerate(S[i:]) if y == 'R']:
            findg(i+j,j)
        for j in [x for x, y in enumerate(S[i:]) if y == 'G']:
            findr(i+j,j)
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
count=0
for i in range(0,n-3):
    s1=s[i]
    for j in range(i+1,n-1):
        s2=s[j]
        if s1!=s2:
            if s1=="R" and s2=="G":
                s3="B"
            elif s1=="G" and s2=="R":
                s3="B"
            elif s1=="R" and s2=="B":
                s3="G"
            elif s1=="B" and s2=="R":
                s3="G"
            elif s1=="B" and s2=="G":
                s3="R"
            elif s1=="G" and s2=="B":
                s3="R"
            count=count+s[j+1:].count(s3)
            if j+j-i+1<=n: 
                if s[j+j-i]==s3:
                    count=count-1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect
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)
Rs = set(R)
Bs = set(B)
Gs = set(G)
res = 0
for i in range(n):
  if s[i] == 'R':
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(B, G[j])
      res += len(B) - y
      sss.add(2*G[j]-i)
    res -= len(sss & Bs)
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(G, B[j])
      res += len(G) - y
      sss.add(2*B[j]-i)
    res -= len(sss & Gs)
  elif s[i] == 'G':
    x = bisect.bisect_left(B, i)
    sss = set()
    for j in range(x, len(B)):
      y = bisect.bisect_left(R, B[j])
      res += len(R) - y
      sss.add(2*B[j]-i)
    res -= len(sss & Rs)
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(B, R[j])
      res += len(B) - y
      sss.add(2*R[j]-i)
    res -= len(sss & Bs)
  else:
    x = bisect.bisect_left(G, i)
    sss = set()
    for j in range(x, len(G)):
      y = bisect.bisect_left(R, G[j])
      res += len(R) - y
      sss.add(2*G[j]-i)
    res -= len(sss & Rs)
    x = bisect.bisect_left(R, i)
    sss = set()
    for j in range(x, len(R)):
      y = bisect.bisect_left(G, R[j])
      res += len(G) - y
      sss.add(2*R[j] - i)
    res -= len(sss & Gs)
print(res)

Submission status: Time Limit Exceeded

Solution code:
def calc_double(n):
    return n * 2

def main():
    N = int(input())
    S = input()
    r = []
    g = []
    b = []
    answer = 0

    for i in range(len(S)):
        # print(S[i])
        if S[i] == 'R':
            r.append(i)
            continue
        if S[i] == 'G':
            g.append(i)
            continue
        b.append(i)


    # print(r)
    # print(g)
    # print(b)
    # print()
    rg = [(i+j) for i in r for j in g]
    gb = [(i+j) for i in g for j in b]
    br = [(i+j) for i in b for j in r]

    # for i in r:
    #     if i in gb: answer += 1
    #
    # for i in g:
    #     if i in br: answer += 1
    #
    # for i in b:
    #     if i in rg: answer += 1
    #
    # print("answer")
    # print(answer)
    # print()

    r = list(map(calc_double, r))
    g = list(map(calc_double, g))
    b = list(map(calc_double, b))


    for i in gb:
        if i in r: answer += 1

    for i in br:
        if i in g: answer += 1

    for i in rg:
        if i in b: answer += 1
    #
    # print("answer")
    # print(answer)
    # print()
    #
    # rg.sort()
    # print(rg)
    # print(b)
    # print()
    # gb.sort()
    # print(gb)
    # print(r)
    # print()
    # br.sort()
    # print(br)
    # print(g)
    #
    # print(len(set(rg) & set(b)))
    # print(len(set(gb) & set(r)))
    # print(len(set(br) & set(g)))

    print(len(r) * len(g) * len(b) - answer)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

count =  0

lri = [i for i, rgb in enumerate(s) if rgb == 'R']
lgi = [i for i, rgb in enumerate(s) if rgb == 'G']
lbi = [i for i, rgb in enumerate(s) if rgb == 'B']

for r, g, b in list(itertools.product(lri, lgi, lbi)):
    l = sorted([r, g, b])
    if l[2] - l [1] != l[1] - l[0]:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as c

def cond1(a,b,c):
    return a != b and b != c and c != a

def cond2(i,j,k):
    return j-i != k - j

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

comb_N = list(c(range(N), 3))

C = [1 for i in range(len(comb_N)) if cond1(S[comb_N[i][0]], S[comb_N[i][1]], S[comb_N[i][2]]) and cond2(*comb_N[i])]
print(sum(C))

Submission status: Time Limit Exceeded

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

diff_dic = {}
diff_dic[('R', 'G')] = []
diff_dic[('R', 'B')] = []
diff_dic[('G', 'R')] = []
diff_dic[('G', 'B')] = []
diff_dic[('B', 'G')] = []
diff_dic[('B', 'R')] = []

R_lst = [0] * N
G_lst = [0] * N
B_lst = [0] * N

r_cnt = 0
g_cnt = 0
b_cnt = 0

cnt = 0
for i in range(N):
    if S[i] == 'R':
        r_cnt += 1
    elif S[i] == 'G':
        g_cnt += 1
    else:
        b_cnt += 1

    R_lst[i] = r_cnt
    G_lst[i] = g_cnt
    B_lst[i] = b_cnt

    for j in range(i):
        d = i - j
        if (S[i] == 'R' and S[j] == 'G') or (S[i] == 'G' and S[j] == 'R'):
            if j - 1 >=0 and B_lst[j - 1] > 0:
                cnt += B_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'B':
                    cnt -= 1

        if (S[i] == 'G' and S[j] == 'B') or (S[i] == 'B' and S[j] == 'G'):
            if j - 1 >=0 and R_lst[j - 1] > 0:
                cnt += R_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'R':
                    cnt -= 1

        if (S[i] == 'B' and S[j] == 'R') or (S[i] == 'R' and S[j] == 'B'):
            if j - 1 >= 0 and G_lst[j - 1] > 0:
                cnt += G_lst[j - 1]
                if j - d >= 0 and S[j - d] == 'G':
                    cnt -= 1

print(cnt)

Submission status: Time Limit Exceeded

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

Ans = 0
for i in range(N):
  s = S[i]
  a = 0
  b = 0
  for j in range(i+1,N):
    t = S[j]
    if s != t:
      L = ["R","G","B"]
      L.remove(s)
      if t == L[0]:
        a +=1
      else:
        b +=1
  Ans += a*b

for i in range(N):
  for j in range(i+1,N):
    s = S[i]
    t = S[j]
    k = 2*j-i
    if k <= N-1:
      u = S[k]
      if s != t and t != u and u != s:
        Ans -=1
print(Ans)

Submission status: Time Limit Exceeded

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

counta = 0
i = 0

while i < N:
    j = i + 1
    while j < N:
        if S[j] != S[i]:
            k = j + 1
            while k < N:
                if k - j != j - i and S[k] != S[i] and S[k] != S[j]:
                    counta += 1
                k += 1
        j += 1
    i += 1

print(counta)

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


result = 0
result_list = []
for r_index in r_list:
    for g_index in g_list:
        for b_index in b_list:
            result_list.append([r_index, g_index, b_index])

minus_count = 0
for comb in result_list:
    comb = sorted(comb)
    if comb[2] - comb[1] == comb[1] - comb[0]:
        minus_count +=1
print(len(result_list)-minus_count)

Submission status: Time Limit Exceeded

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


ans =  0
for i in range(N):
    s1 = S[i]
    for j in range(i+1,N):
        if S[j] != s1:
            a=["R","G","B"]
            a.remove(S[j])
            a.remove(s1)
            ans += S[j:].count(a[0])
            if j+(j-i)<N:
                if S[j+(j-i)]==a[0]:
                    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
S = read_str()

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

rap = r.append
gap = g.append
bap = b.append
for i, s in enumerate(S):
    if s == 'R': rap(i)
    elif s == 'G': gap(i)
    else: bap(i)
rgb = [r, g, b]

# print(r, g, b)

def bisect_find_lt(a, x):
    'Find rightmost value less than x'
    i = bisect.bisect_left(a, x)
    if i:
        return i-1
    raise ValueError

def bisect_find_gt(a, x):
    'Find leftmost value greater than x'
    i = bisect.bisect_right(a, x)
    if i != len(a):
        return i
    raise ValueError

def solve():
    ans = 0
    lb = len(b)
    lg = len(g)
    lr = len(r)
    if len(r) == 0 or len(g) == 0 or len(b) == 0:
        return ans
    for i, ng in enumerate(g):
        try:
            ri = bisect_find_lt(r, ng)
            bi = bisect_find_gt(b, ng)
            # print(ri, bi)
            for rj in r[0 : ri + 1]:
                for bj in b[bi : lb]:
                    # print(rj, ng, bj)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(g):
        try:
            ri = bisect_find_lt(b, ng)
            bi = bisect_find_gt(r, ng)
            # print(ri, bi)
            for rj in b[0 : ri + 1]:
                for bj in r[bi : lr]:
                    # print(rj, ng, bj)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(b):
        try:
            ri = bisect_find_lt(g, ng)
            bi = bisect_find_gt(r, ng)
            # print(ri, bi)
            for rj in g[0 : ri + 1]:
                for bj in r[bi: lr]:
                    # print(rj, ng, bi)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(b):
        try:
            ri = bisect_find_lt(r, ng)
            bi = bisect_find_gt(g, ng)
            # print(ri, bi)
            for rj in r[0 : ri + 1]:
                for bj in g[bi : lg]:
                    # print(rj, ng, bi)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(r):
        try:
            ri = bisect_find_lt(b, ng)
            bi = bisect_find_gt(g, ng)
            # print(ri, bi)
            for rj in b[0 : ri + 1]:
                for bj in g[bi : lg]:
                    # print(rj, ng, bi)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass

    for i, ng in enumerate(r):
        try:
            ri = bisect_find_lt(g, ng)
            bi = bisect_find_gt(b, ng)
            # print(ri, bi)
            for rj in g[0 : ri + 1]:
                for bj in b[bi : lb]:
                    # print(rj, ng, bi)
                    if ng - rj != bj - ng:
                        ans += 1
        except ValueError:
            pass


    return ans

if __name__ == "__main__":
    print(solve())

Submission status: Time Limit Exceeded

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

r_index = []
g_index = []
b_index = []

for i in range(N):
    if S[i] == 'R':
        r_index.append(i)

    if S[i] == 'G':
        g_index.append(i)

    if S[i] == 'B':
        b_index.append(i)

res = 0

# print(len(r_index), len(g_index), len(b_index))

max_numbers = len(r_index) * len(g_index) * len(b_index)


def cnt(x, y, z):
    res = 0
    for xi in x:
        for yi in y:
            for zi in z:
                if yi - xi == zi - yi:
                    res += 1
    return res


ans = []

ans.append(cnt(r_index, g_index, b_index))
ans.append(cnt(r_index, b_index, g_index))
ans.append(cnt(b_index, r_index, g_index))

# print(ans)
print(max_numbers - sum(ans))

Submission status: Time Limit Exceeded

Solution code:
def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count

def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return list(map(str, input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
  n = int(input())
  if n < 4:
    print(0)
    exit()
  ans = 0
  s = list(input())
  for i, a in enumerate(s):
    j = i+1
    k = i+2
    for bi, b in enumerate(s[j:]):
      if a == b:
        continue
      bb = bi+j
      for ci, c in enumerate(s[k:]):
        if a == c or b == c:
          continue
        cc = ci+k
        if i < bb < cc:
          if bb - i == cc - bb:
            continue
          ans += 1
  print(ans)          
import math
import fractions
import collections
import itertools
from functools import reduce
main()

Submission status: Time Limit Exceeded

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

main()

Submission status: Time Limit Exceeded

Solution code:
def binary_search(list,target):

	result = -1
	left = 0
	right = len(list) - 1

	while left <= right:
		center = int((left + right)/2)
		if list[center] == target:
			result = center
			break
		elif list[center] < target:
			left = center + 1
		elif list[center] > target:
			right = center - 1

	if result == -1:
		return result
	else:
		return result


if __name__=="__main__":
    n=int(input())
    s=list(input())
    rlist=[]
    glist=[]
    blist=[]
    ans=0
    if len(s)<=3:
        print(0)
        exit()
    for i in range(n):
        if s[i]=="R":
            rlist.append(i)
        elif s[i]=="G":
            glist.append(i)
        else:
            blist.append(i)
    rlist=tuple(rlist)
    blist=tuple(blist)
    glist=tuple(glist)
    if len(rlist)==0 or len(glist)==0 or len(blist)==0:
        print(0)
        exit()
    ans+=len(rlist)*len(glist)*len(blist)
    for i in glist:
        for j in blist:
            if i>j :
                if binary_search(rlist,2*i-j)!=-1:
                    ans-=1
                if binary_search(rlist,2*j-i)!=-1:
                    ans-=1
                if (i+j)/2 in rlist:
                    ans-=1
            else:
                if binary_search(rlist,2*i-j)!=-1:
                    ans-=1
                if binary_search(rlist,2*j-i)!=-1:
                    ans-=1
                if (i+j)/2 in rlist:
                    ans-=1

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
cnt=0
if n<=2:
    print(0)
    quit()
import itertools           
for i,j,k in itertools.combinations_with_replacement(range(0,n), 3):
    si,sj,sk=s[i],s[j],s[k]
    if 2*j!=i+k and si!=sj and si!=sk and sj!=sk:
        cnt+=1
print(cnt)

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)

ans=len(r)*len(g)*len(b)
for i in r:
    for j in g:
        for k in b:
            t=[i,j,k]
            t.sort()
            x,y,z=t
            if y-x == z-y:
                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 i in range(N):
    for r in R:
        a0 = r - i - i
        a1 = r - i
        a2 = r + i
        a3 = r + i + i
        if (binary_search(G,a0) != -1 and binary_search(B,a1) != -1) or (binary_search(B,a0) != -1 and binary_search(G,a1) != -1):
            s -= 1
        if (binary_search(G,a2) != -1 and binary_search(B,a1) != -1) or (binary_search(B,a2) != -1 and binary_search(G,a1) != -1):
            s -= 1
        if (binary_search(G,a2) != -1 and binary_search(B,a3) != -1) or (binary_search(B,a2) != -1 and binary_search(G,a3) != -1):
            s -= 1
print(s)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
s = input()
count = 0
list1 = []
list = []
list2= []
for i in range(1, len(s)+1):
    list.append(i)
for c in combinations(list, 3):
    list1.append(c)
for item in list1:
    if (item[1]-item[0]) != (item[2]-item[1]):
        list2.append(item)
for k in list2:
    if s[k[0]-1]!=s[k[1]-1] and s[k[0]-1]!=s[k[2]-1] and s[k[1]-1]!=s[k[2]-1]:
        count= count+1
print(count)

Submission status: Time Limit Exceeded

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

    d = 0
    for k in range(2,N):
        sk = S[k]
        for j in range(1,k):
            sj = S[j]
            if sk != sj:
                for i in range(j):
                    si = S[i]
                    if (si != sj) & (si != sk) & (j-i != k-j):
        
                        d += 1

    print(d)


main()

Submission status: Time Limit Exceeded

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

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

for i in r:
    for j in g:
        for k in b:
            num = [i, j, k]
            num.sort()
            if num[1]-num[0]==num[2]-num[1]:
                continue
            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] and S[i] != 'R' and S[j] != 'R':
          ans += S[j:N].count('R')
          if j+(j-i) < N and S[j+(j-i)] == 'R':
            ans -= 1
        elif S[i] != S[j] and S[i] != 'G' and S[j] != 'G':
          ans += S[j:N].count('G')
          if j+(j-i) < N and S[j+(j-i)] == 'G':
            ans -= 1
        elif S[i] != S[j] and S[i] != 'B' and S[j] != 'B':
          ans += S[j:N].count('B')
          if j+(j-i) < N and S[j+(j-i)] == 'B':
            ans -= 1
print(ans)

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

r_pos = []
g_pos = []
b_pos = []

for i,ss in enumerate(s):
    if ss == 'R':
        r_pos.append(i)
    elif ss == 'G':
        g_pos.append(i)
    elif ss == 'B':
        b_pos.append(i)

total = 0
if r_pos is None or g_pos is None or b_pos is None:
    print(total)
    exit()

for rr in r_pos:
    for gg in g_pos:
        for bb in b_pos:
            l = [rr,gg,bb]
            l.sort()
            if l[1] - l[0] != l[2] - l[1]:
                total += 1

print(total)

Submission status: Time Limit Exceeded

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


    su = 0
    for i, si in enumerate(S[:-2]):
        for j, sj in enumerate(S[i+1:-1]):
            if si == sj:
                continue
            tmps = S[i+j+2:]
            if len(tmps) <= j:
                pass
            else:
                if tmps[j] not in [sj, si]:
                    su -= 1

            tmps2 = [s for s in tmps if s not in [si, sj]]
            su += len(tmps2)
    print(su)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# ABC162 D

import math

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

    res = 0

    for i in range(len(s)):
        firstChar = s[i]

        for j in range(i+1, len(s)):
            if s[j] != firstChar:
                secondChar = s[j]
                
                for k in range(j+1, len(s)):
                    if j - i == k - j:
                        continue
                    elif s[k] != firstChar and s[k] != secondChar:
                        thirdChar = s[k]
                        res += 1

    print(res)                        

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
r = [0] * n
g = [0] * n
b = [0] * n
R = 0
G = 0
B = 0
for i in range(n-1, -1, -1):
  r[i] = R
  g[i] = G
  b[i] = B
  if s[i] == 'R':R+=1
  if s[i] == 'G':G+=1
  if s[i] == 'B':B+=1

ans = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    if (s[i] == 'R' and s[j] == 'G') or (s[i] == 'G' and s[j] =='R'):
      ans += b[j]
      if 2*j - i < n:
        if s[2*j-i] == 'B': ans -= 1
    if (s[i] == 'G' and s[j] == 'B') or (s[i] == 'B' and s[j] =='G'):
      ans += r[j]
      if 2*j - i < n:
        if s[2*j-i] == 'R': ans -= 1
    if (s[i] == 'B' and s[j] == 'R') or (s[i]== 'R' and s[j] =='B'):
      ans += g[j]
      if 2*j -i < n:
        if s[2*j-i] == 'G': ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
s = input()
comb = combinations(list(range(0, n)), 3)
ans = 0
for i, j, k in list(comb):
    if s[i] != s[j] and s[i] != s[k] and s[j] != s[k] and j - i != k - j:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

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

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

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

l = list(S)
ans = 0
for a,b,c in itertools.combinations_with_replacement(range(N), 3):
  if (l[a]!=l[b]) and (l[b]!=l[c]) and (l[c]!=l[a]) and (b-a!=c-b):
    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)