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, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]


r_list= []
g_list = []
b_list = []
def length(t):
    if t == "R":
        return len(r_list)
    elif t == "G":
        return len(g_list)
    else:
        return len(b_list)

def rgb_list(t):
    if t == "R":
        return r_list
    elif t == "G":
        return g_list
    else:
        return b_list
n = i()
s = s()

for i, t in enumerate(s):
    if t == "R":
        r_list.append(i)
    elif t == "G":
        g_list.append(i)
    else:
        b_list.append(i)

rgb = ["R", "G", "B"]
s_start = 0
t_start = 0
for color in itertools.permutations(rgb, 3):
    f_len = length(color[0])
    s_len = length(color[1])
    t_len = length(color[2])
    f_list = rgb_list(color[0])
    s_list = rgb_list(color[1])
    t_list = rgb_list(color[2])

    s_start = 0
    for first in f_list:
        t_start = 0
        flag = 0
        for s_i in range(s_start, len(s_list)):
            second = s_list[s_i]
            if first < second:
                if flag == 0:
                    s_start = s_i
                    flag += 1
                interval = second - first
                if first + 2 * interval in set(t_list):
                    ans -= 1
                for t_i in range(t_start, len(t_list)):
                    third = t_list[t_i]
                    if second < third:
                        ans += t_len - t_i
                        t_start = t_i
                        break
                else:
                    break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]


r_list= []
g_list = []
b_list = []
def length(t):
    if t == "R":
        return len(r_list)
    elif t == "G":
        return len(g_list)
    else:
        return len(b_list)

def rgb_list(t):
    if t == "R":
        return r_list
    elif t == "G":
        return g_list
    else:
        return b_list
n = i()
s = s()

for i, t in enumerate(s):
    if t == "R":
        r_list.append(i)
    elif t == "G":
        g_list.append(i)
    else:
        b_list.append(i)

rgb = ["R", "G", "B"]
t_start = 0
for color in itertools.permutations(rgb, 3):
    f_len = length(color[0])
    s_len = length(color[1])
    t_len = length(color[2])
    f_list = rgb_list(color[0])
    s_list = rgb_list(color[1])
    t_list = rgb_list(color[2])
    for first in f_list:
        t_start = 0
        for s_i, second in enumerate(s_list):
            if first < second:
                interval = second - first
                if first + 2 * interval in set(t_list):
                    ans -= 1
                for t_i in range(t_start, len(t_list)):
                    third = t_list[t_i]
                    if second < third:
                        ans += t_len - t_i
                        t_start = t_i
                        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
line1 = int(input())



line2 = input()
literal = list(line2)


cnt =0
jud = []
for i in range(line1):
  for s in range(line1):
    for t in range(line1):
      jud = [literal[i],literal[s], literal[t]]
      if (jud[0] !=jud[1]) and (jud[1]!=jud[2]) and (jud[0] !=jud[2]):
        if s -i !=t-s and i<s and s<t:
          cnt +=1
          
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(str(input()))
a =[]
for k in range(0,n):
  for j in range(0,k):
    if s[j] != s[k]:
      for i in range(0,j):
        if s[i] != s[j] and s[k] != s[i] and j-i != k - j:
          a.append(i)

print(len(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(str(input()))
a =[]
for k in range(0,n):
  for j in range(0,k):
    for i in range(0,j):
      if s[i] != s[j] and s[j] != s[k] and s[k] != s[i] and j-i != k - j:
        a.append(i)

print(len(a))

Submission status: Time Limit Exceeded

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


count_r = S.count('R')
count_g = S.count('G')
count_b = S.count('B')

total = count_r * count_g * count_b


for gap in range(1, N//2+1):
    for i in range(N - 2):
        j = i + gap
        k = j + gap
        if k >= N:
            continue
        if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
            total -= 1

print(total)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import math

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


count_r = S.count('R')
count_g = S.count('G')
count_b = S.count('B')

total = count_r * count_g * count_b

for i in range(1, len(S)):
    for j in range(len(S)):
        if j+i+i >= len(S):
            continue
        if S[j] != S[j+i] and S[j+i] != S[j+i+i] and S[j] != S[j+i+i]:
            total -= 1

print(total)

Submission status: Time Limit Exceeded

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

index_R = [i + 1 for i, x in enumerate(s) if x == "R"]
index_B = [i + 1 for i, x in enumerate(s) if x == "B"]
index_G = [i + 1 for i, x in enumerate(s) if x == "G"]

ans = len(index_R) * len(index_B) * len(index_G)

for i in index_R:
    for j in index_B:
        if (i + j) / 2 in index_G:
            ans -= 1
        if min(i, j) - abs(i - j) in index_G:
            ans -= 1
        if max(i, j) + abs(i - j) in index_G:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

rgb = set(['R', 'G', 'B'])

left = {}
right = {}
for c in rgb:
    left[c] = [0] * n
    right[c] = [0] * n

for i in range(0, n-1):
    for c in rgb:
        left[c][i+1] = left[c][i] + (1 if s[i] == c else 0)

for k in reversed(range(1, n)):
    for c in rgb:
        right[c][k-1] = right[c][k] + (1 if s[k] == c else 0)

ans = 0
for j in range(1, n-1):
    c = s[j]
    rst = rgb - set([c])
    [a, b] = rst
    ans += left[a][j] * right[b][j]
    ans += left[b][j] * right[a][j]
    for i in range(0, j):
        if s[i] == a and 2*j - i < n and s[2*j-i] == b:
            ans -= 1
        if s[i] == b and 2*j - i < n and s[2*j-i] == a:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
R=[]
G=[]
B=[]

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

ans=len(R)*len(G)*len(B)

for r in R:
  for g in G:
    for b in B:
      list=[r,g,b]
      list.sort()
      if list[2]-list[1]==list[1]-list[0]:
        ans -=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
input = sys.stdin.readline
N = int(input())
S = input().strip()
assert 1 <= N <= 4000
assert N == len(S)
A = S.count('R')*S.count('G')*S.count('B')
total = 0
for j in range(1, N-1):
    for i in range(j):
        k = 2*j-i
        if k < len(S):
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                total += 1
# print(c["R"]*c["G"]*c["B"]-total)
print(A - total)

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
input = sys.stdin.readline
N = int(input())
S = input().strip()
assert 1 <= N <= 4000
assert N == len(S)
r = S.count("R")
g = S.count("G")
b = S.count("B")
total = 0
for j in range(1, N-1):
    for i in range(j):
        k = 2*j-i
        if k < len(S):
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                total += 1
# print(c["R"]*c["G"]*c["B"]-total)
print(r*g*b - total)

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
input = sys.stdin.readline
N = int(input())
S = list(input().strip())
assert 1 <= N <= 4000
assert N == len(S)
c = collections.Counter(S)
total = 0
for j in range(1, N-1):
    for i in range(j):
        k = 2*j-i
        if k < len(S):
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                total += 1
print(c["R"]*c["G"]*c["B"]-total)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
S = list(input().strip())
assert 1 <= N <= 4000
assert N == len(S)
r = S.count("R")
g = S.count("G")
b = S.count("B")
total = 0

for j in range(1, N-1):
    for i in range(j):
        k = 2*j-i
        if k < len(S):
            if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
                total += 1
print(r*g*b-total)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(input())
R = []
B = []
G = []
for i in range(N):
  if A[i] == "R":
    R.append(i)
  elif A[i] == "G":
    G.append(i)
  else:
    B.append(i)
ans = 0
for i in range(len(R)):
  for j in range(len(G)):
    for k in range(len(B)):
      if (max(R[i], G[j], B[k]) + min(R[i], G[j], B[k]))/2 != (R[i]+G[j]+B[k])/3:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r,g,b =0,0,0
for i in range(len(s)):
    if s[i]=='R':
        r+=1
    elif s[i]=='G':
        g+=1
    elif s[i]=='B':
        b+=1
cnt =0
for j in range(1,len(s)-1):
    for i in range(j):
        k = 2*j-i
        if k<len(s):
            if s[i]!=s[j] and s[i]!=s[k] and s[j]!=s[k]:
                cnt+=1
print(r*g*b-cnt)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
# 15:00~
# N=4
# S="RRGB"
# j-i!=k-j
# i,j,kで文字が違う。
# print(S[1])

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S1 = input()
S = [(a, p) for p, a in enumerate(S1)]
 
count = 0
for i in range(N):
    for k in range(i+1,N):
        if S[i][0] != S[k][0]:
            RGB = ['R','G','B']
            RGB.remove(S[i][0])
            RGB.remove(S[k][0])
            count += S1.count(RGB[0],k+1,N)
            if 2*k-i < N:
                if S[2*k-i][0]!= S[i][0] and S[2*k-i][0]!=S[k][0]:
                    count -= 1 
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S1 = input()
S = [(a, p) for p, a in enumerate(S1)]

count = 0
for i in range(N):
    for k in range(i+1,N):
        if S[i][0] != S[k][0]:
            RGB = ['R','G','B']
            RGB.remove(S[i][0])
            RGB.remove(S[k][0])
            count += S1.count(RGB[0],k+1,N)
            if 2*k-i < N:
                if S[2*k-i][0]!= S[i][0] and S[2*k-i][0]!=S[k][0]:
                    count -= 1 
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S1 = input()
S = [(a, p) for p, a in enumerate(S1)]
count = 0
for i in range(N):
    for k in range(i+1,N):
        if S[i][0] != S[k][0]:
            count += (N-k-1)-S1.count(S[i][0],k+1,N)-S1.count(S[k][0],k+1,N)
            if 2*k-i < N:
                if S[2*k-i][0]!= S[i][0] and S[2*k-i][0]!=S[k][0]:
                    count -= 1 
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
r = S.count('R')
g = S.count('G')
b = S.count('B')
count = r * g * b
for i in range(N):
    for j in range(i+1,N-1):
        if 2*j - i <= N-1:
            l = set([S[i],S[j],S[2*j-i]])
            if len(l) == 3:
                count -= 1
print(count)

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
s = list(S)
r = 0
g = 0
b = 0
for i in range(N):
    if s[i] == 'R':
        r = r + 1
    if s[i] == 'G':
        g = g + 1
    if s[i] == 'B':
        b = b + 1
p = 0
for i in range(N-1):
    for j in range(i+1,N):
        if 2*j-i < N:
            if s[2*j-i] == s[i] or s[2*j-i] == s[j] or s[i] == s[j]:
                p = p
            else:
                p = p+1
print(r*g*b-p)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
s = list(S)
r = 0
g = 0
b = 0
for i in range(N):
    if s[i] == 'R':
        r = r + 1
    if s[i] == 'G':
        g = g + 1
    if s[i] == 'B':
        b = b + 1
p = 0
for i in range(N-1):
    for j in range(i+1,N):
        if 2*j-i < N:
            if s[j+j-i] != s[i] and s[j+j-i] != s[j] and s[i] != s[j]:
                p = p+1
print(r*g*b-p)

Submission status: Time Limit Exceeded

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

an = s.count('R') *s.count('G') *s.count('B')

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

print(an)

Submission status: Time Limit Exceeded

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

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

    print(count)


rgb_triplets()

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)
    elif s[i] == 'B':
        B.append(i)
c = 0
for r in R:
    for g in G:
        for b in B:
            [i,j,k] = sorted([r,g,b])
            if j-i != k-j:
                c = c+1
print(c)

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)
    elif s[i] == 'B':
        B.append(i)
c = 0
for r in R:
    for g in G:
        for b in B:
            [i,j,k] = sorted([r,g,b])
            if len(set([i,j,k])) == 3 and j-i != k-j:
                c = c+1
print(c)

Submission status: Time Limit Exceeded

Solution code:
def rgb_triplets():
    from itertools import combinations
    N = int(input())
    S = input()

    indices = [i for i in range(0, N)]
    triples = combinations(indices, 3)

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

    print(count)


rgb_triplets()

Submission status: Time Limit Exceeded

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

indices = [i for i in range(0, N)]
triples = combinations(indices, 3)

count = 0
for triple in triples:
    i, j, k = triple
    if i < j < k:
        if j - i != k - j:
            if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
                count += 1

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
inp = input()
ans = 0
for i in range(1, num+1):
  for j in range(i+1, num+1):
    for k in range(j+1, num+1):
      if inp[i-1] != inp[j-1] and inp[j-1] != inp[k-1] and inp[k-1] != inp[i-1]:
        if j-i != k-j:
          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')

ans = r * g * b
for i in range(1, (n // 2) + 1):
    for j in range(0, n - (2 * i) + 1):
        if j + (2 * i) >= n:
            break
        if ((s[j] != s[j + i]) & (s[j] != s[j + (2 * i)]) & (s[i + j] != s[j + (2 * i)])):
            ans -= 1
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r_list = set([])
b_list = set([])
g_list = set([])

for i in range(n):
    if s[i] == 'R':
        r_list.add(i)
    elif s[i] == 'B':
        b_list.add(i)
    else:
        g_list.add(i)
        
ans = 0
for r in r_list:
    for b in b_list:
        ans += len(g_list - {2 * b - r, 2 * r - b, (b + r) / 2})
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
str=input()
count=str.count("R")*str.count("G")*str.count("B")
count0=0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if 2*j-i<n:
            k=2*j-i
            if str[k]!=str[i] and str[k]!=str[j] and str[i]!=str[j]:
                count0=count0+1
print(count-count0)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
str=input()
s=''
count=0
for i in range(0,n-3):
    for j in range(i+1,n-1):
        s=str[i]
        if str[j] not in s:
            s=str[i]+str[j]
            for k in range(j+1,n):
                if k-j != j-i:
                    if str[k] not in s:
                        count=count+1
print(count)

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

Submission status: Time Limit Exceeded

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

#条件1を満たす(i,j,k)の組→R,G,Bの選び方
r,g,b = S.count('R'),S.count('G'),S.count('B')

ans = r*g*b

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

print(ans)

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(N):
#    print('i={}'.format(i))
    for j in range(i+1,N):
#        print('j={}'.format(j))
        if S[i] != S[j]:
            for k in range(j+1,N):
#                print(k)
                if S[i] != S[k] and S[j] != S[k]:
                    if j-i != k-j:
                        cnt +=1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

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

#aa = list()
ans = len(R) * len(G) * len(B)
for (i, j) in combinations(range(N - 1), 2):
    k = j*2 - i
    if k < N and S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
        ans -= 1
#        aa.append((i, j, k))

print(ans)
#print(aa)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import deque
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline

N = I()
S = input()
cnt = 0
l = [S[i] for i in range(N)]
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            if l[i]==l[j] or l[j]==l[k] or l[k]==l[i] or j-i==k-j:
                continue
            cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import deque
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)
input = sys.stdin.readline

N = I()
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] or S[j]==S[k] or S[k]==S[i] or j-i==k-j:
                continue
            cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

r_i = [i for i, x in enumerate(s) if x=='R']
g_i = [i for i, x in enumerate(s) if x=='G']
b_i = [i for i, x in enumerate(s) if x=='B']

ans = 0

for i in range(len(r_i)):
    for j in range(len(g_i)):
        for k in range(len(b_i)):
            ijk = [r_i[i], g_i[j], b_i[k]]
            ijk.sort()
            if ijk[1] - ijk[0] != ijk[2] - ijk[1]:
                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")

ret = r * g * b
for i in range(N - 2):
    for j in range(1, N):
        if i + j + j >= N:
            break
        a = S[i]
        b = S[i + j]
        c = S[i + j + j]
        if a != b and b != c and c != a:
            ret -= 1
print(ret)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
r = 0
g = 0
b = 0
for s in S:
    if s == "R":
        r += 1
    elif s == "G":
        g += 1
    else:
        b += 1

ret = r * g * b
for i in range(N - 2):
    for j in range(1, N - i):
        if N > i + j + j:
            a = S[i]
            b = S[i + j]
            c = S[i + j + j]
            if a != b and b != c and c != a:
                ret -= 1
print(ret)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
r = S.count("R")
g = S.count("G")
b = S.count("B")

ret = r * g * b
for i in range(N - 2):
    for j in range(1, N):
        if N > i + j + j:
            a = S[i]
            b = S[i + j]
            c = S[i + j + j]
            if a != b and b != c and c != a:
                ret -= 1
print(ret)

Submission status: Time Limit Exceeded

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

r=0
g=0
for i in range(N):
  if S[i]=='R':
    r+=1
  if S[i]=='G':
    g+=1
print(r*g*(N-r-g)-count)

Submission status: Time Limit Exceeded

Solution code:
def D():
    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 = 0
    for i in r:
        for j in g:
            for k in b:
                if abs(i-j) != abs(k-j) and abs(i-k)!=abs(k-j) and abs(j-i)!=abs(k-i):
                    ans += 1
    print(ans)

D()

Submission status: Time Limit Exceeded

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

a=l.count("R")
b=l.count("G")
c=l.count("B")

ans=0
d=0
e=0
f=0

for _ in range(a):
  x=l.find("R",d)
  d=x+1
  for _ in range(b):
    y=l.find("G",e)
    e=y+1
    if e==l.rfind("G")+1:
      e=0
    for _ in range(c):
      z=l.find("B",f)
      f=z+1
      if f==l.rfind("B")+1:
        f=0
      if (abs(x-y)==abs(y-z)) or (abs(x-z)==abs(z-y)) or (abs(y-x)==abs(x-z)):
        ans+=1
    
print(a*b*c-ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S= input()
count = 0
R,G,B = [],[],[]
for i in S:
    if i == 'R':
        R.append(count)
    elif i == 'G':
        G.append(count)
    else:
        B.append(count)
    count += 1
sub = 0
for r in R:
    for g in G:
        for b in B:
            if b==2*r-g or b==2*g-r or b==(r+g)/2:
                sub +=1
ans = len(R)*len(G)*len(B) -sub
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S= input()
ret = 0
count = 0
R,G,B = [],[],[]
for i in S:
    if i == 'R':
        R.append(count)
    elif i == 'G':
        G.append(count)
    else:
        B.append(count)
    count += 1

for r in R:
    for g in G:
        i = min(r,g)
        j = max(r,g)
        for b in B:
            if i < b < j and b != (i+j)/2:
                ret +=1
            if j < b and b != 2*j -i:
                ret +=1
            if b < i and b != 2*i-j:
                ret += 1
print(ret)