Problem:


Score : 400 points

Problem Statement

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

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

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

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

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

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

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

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800



Sample input:
4
RRGB


Sample output:
1

Submission status: Time Limit Exceeded

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

input_line = input()

input_line = input()
Line = list(input_line)

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

ans = 0

for i in range(len(Line)):
    if Line[i] == "R":
        R.append(i)
    if Line[i] == "G":
        G.append(i)
    if Line[i] == "B":
        B.append(i)

# print(R,G,B)
 
for i in itertools.product(R,G,B):
    i = sorted(i)
    # print(i)
    if (i[0]-i[1]) != (i[1]-i[2]):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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

cnt = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        c = Counter(s[j:])
        total = sum(c.values()) - 1
        cnt += total - c[s[i]] - c[s[j]]
        w = j - i
        if j+w < n and (s[j+w] != s[j] and s[j+w] != s[i]):
            cnt -= 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
# S = [s for s in S]
cnt = 0

# def my_index_multi(l, x):
#     return [i for i, _x in enumerate(l) if _x == x]
# R = my_index_multi(S, 'R')
# G = my_index_multi(S, 'G')
# B = my_index_multi(S, 'B')
R = []
G = []
B = []
for i, s in enumerate(S):
    if s=='R':
        R.append(i)
    elif s=='G':
        G.append(i)
    else:
        B.append(i)

for r in R:
    for g in G:
        for b in B:
            l = [r, g, b]
            l.sort()
            if (l[1]-l[0])!=(l[2]-l[1]):
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

cnt = 0
l = ["R", "G", "B"]
for i in range(len(S)-2):
    for j in range(i, len(S)):
        if S[i] != S[j]:
            for k in range(j, len(S)):
                s = [S[i], S[j], S[k]]
                if set(s) == set(l):
                    if j-i != k-j:
                        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

count = 0
char_candidates = "RGB"

for i in range(len(s)):
    for j in range(i+1, len(s)):
        i_char = s[i]
        j_char = s[j]
        
        if i_char == j_char:
            continue

        # valid_k_char = char_candidates.replace(s[i], "").replace(s[j], "")
        # print(valid_k_char)

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
def get_diff_s(a, b):
    return list({'R', 'G', 'B'} - {a, b})[0]

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

ans = 0
for i in range(n - 3):
    for j in range(i + 1, n - 1):
        if s[i] == s[j]:
            continue
        ds = get_diff_s(s[i], s[j])
        di = j - i
        ans += (s[j + 1:j + di] + s[j + di + 1:]).count(ds)

print(ans)

Submission status: Time Limit Exceeded

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

index = {'R':[], 'G':[], 'B':[]}
for i, s in enumerate(S):
    index[s].append(i)
R=index['R']
G=index['G']
B=index['B']
count=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]:
                count+=1
print(count)

Submission status: Time Limit Exceeded

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

def main():
    n = int(input())
    s = input()
    R = []
    G = []
    B = []
    R_a = R.append
    G_a = G.append
    B_a = B.append

    for i in range(n):
        if s[i] == "R":
            R_a(i + 1)
        elif s[i] == "G":
            G_a(i + 1)
        else:
            B_a(i + 1)
    res = 0
    for r in R:
        for g in G:
            for b in B:
                if r + g != b * 2 and g + b != r * 2 and b + r != g * 2:
                    res += 1
    print(res)
if __name__ == '__main__':
    main()

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

Submission status: Time Limit Exceeded

Solution code:
import math

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

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

for i in range(len(s)):
    if s[i] == "R": R.append(i + 1)
    if s[i] == "G": G.append(i + 1)
    if s[i] == "B": B.append(i + 1)

cnt = 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:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(N-3):
    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]:
                if j - i != k - j:
                    n = n+1

print(n)

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

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right

def runLength(string):
    """文字列strをランレングス圧縮して返す関数
    ex: "0000111101" => [(0,4), (1,4), (0,1), (1,1)]
    """
    res = []
    data, count = string[0], 1
    for i in range(1, len(string)):
        if string[i] == data:
            count += 1
        else:
            res.append((data, count, i))
            data, count = string[i], 1
    res.append((data, count, i))
    return res


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

rl = runLength(S)
keys = [rli[2] for rli in rl]
nrl = len(rl)

ans = 0

for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == S[j]:
            continue
        elif set([S[i], S[j]]) == {"R", "G"}:
            if 2*j - i < N and S[2*j - i] == "B":
                ans += max(sum([rl[x][1] for x in range(bisect_left(keys, j+1), nrl) if rl[x][0] == "B"]) - 1, 0)
            else:
                ans += sum([rl[x][1] for x in range(bisect_left(keys, j+1), nrl) if rl[x][0] == "B"])
        elif set([S[i], S[j]]) == {"B", "G"}:
            if 2*j - i < N and S[2*j - i] == "R":
                ans += max(sum([rl[x][1] for x in range(bisect_left(keys, j+1), nrl) if rl[x][0] == "R"]) - 1, 0)
            else:
                ans += sum([rl[x][1] for x in range(bisect_left(keys, j+1), nrl) if rl[x][0] == "R"])
        else:
            if 2*j - i < N and S[2*j - i] == "G":
                ans += max(sum([rl[x][1] for x in range(bisect_left(keys, j+1), nrl) if rl[x][0] == "G"]) - 1, 0)
            else:
                ans += sum([rl[x][1] for x in range(bisect_left(keys, j+1), nrl) if rl[x][0] == "G"])

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = 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']

count = 0
for r in r_list:
    for g in g_list:
        if g < r:
            continue
        for b in b_list:
            if b < r:
                continue
            m = min(g, b)
            if (m-r) != abs(b-g):
                count += 1

for r in g_list:
    for g in r_list:
        if g < r:
            continue
        for b in b_list:
            if b < r:
                continue
            m = min(g, b)
            if (m-r) != abs(b-g):
                count += 1

for r in b_list:
    for g in r_list:
        if g < r:
            continue
        for b in g_list:
            if b < r:
                continue
            m = min(g, b)
            if (m-r) != abs(b-g):
                count += 1

print(count)

Submission status: Time Limit Exceeded

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

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

result = 0

for k in range(input_nums):
    for j in range(k):
        for i in range(j):
            if(j-i == k-j):
                pass
            elif(input_line[i] == input_line[j]):
                pass
            elif(input_line[i] == input_line[k]):
                pass
            elif(input_line[j] == input_line[k]):
                pass
            else:
                result = result + 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
_ = input()
S = input()
R = []
G = []
B = []
count = 0
for i in range(len(S)):
    if S[i] == "R":
        R.append(i)
    elif S[i] == "G":
        G.append(i)
    else:
        B.append(i)
for r in R:
    for g in G:
        for b in B:
            a, b, c = sorted([r,g,b])
            if abs(b -a) != abs(b -c):
                count += 1
print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
b=input()
sum=0
if a>3:
    for r in range(a-2):
        for g in range(a-1):
            for d in range(a):
                keyw=b[r]+b[g]+b[d]
                if ((g-r)==(d-g))or(r>g)or(g>d):
                    pass
                elif ('R' in keyw)and('G' in keyw)and('B' in keyw):
                    sum+=1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
out = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        if s[j] != s[i]:
            tmpstr = s[j + 1 :  n]
            remain = "RBG".replace(s[i],"").replace(s[j],"")
            if len(remain) == 1:
                num = tmpstr.count(remain) 
                if (j + (j - i)) < len(s):
                    if  s[j + (j - i)]  == remain:
                        num = num - 1
                out += num
print(out)

Submission status: Time Limit Exceeded

Solution code:
import sys
# sys.stdin = open('input.txt')

from math import gcd


n = int(input())
s = input()
i_indices = []
j_indices = []
k_indices = []

for i in range(n):
    if s[i] == 'R':
        i_indices.append(i+1)
    elif s[i] == 'G':
        j_indices.append(i+1)
    else:
        k_indices.append(i+1)

# print(i_indices, len(i_indices))
# print(j_indices, len(j_indices))
# print(k_indices, len(k_indices))

ans = 0

for i in i_indices:
    for j in j_indices:
        for k in k_indices:
            a, b, c = i, j, k
            if b - a != c - b and a < b < c:
                ans += 1
            a, b, c = i, k, j
            if b - a != c - b and a < b < c:
                ans += 1
            a, b, c = j, i, k
            if b - a != c - b and a < b < c:
                ans += 1
            a, b, c = j, k, i
            if b - a != c - b and a < b < c:
                ans += 1
            a, b, c = k, i, j
            if b - a != c - b and a < b < c:
                ans += 1
            a, b, c = k, j, i
            if b - a != c - b and a < b < c:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        for ib in range(ia+1, n-1):
            tb = s[ib]
            t = (ta, tb)
            sa = ib - ia
            if ta == tb:
                pass
            else:
                if 'R' in t and 'G' in t:
                    r += sum(sb[ib+1:])
                    if ib + sa < n:
                        r -= sb[ib + sa]
                elif 'G' in t and 'B' in t:
                    r += sum(sr[ib+1:])
                    if ib + sa < n:
                        r -= sr[ib + sa]
                else:
                    r += sum(sg[ib + 1:])
                    if ib + sa < n:
                        r -= sg[ib + sa]

    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

count = 0

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
data = [input() for i in range(2)]
N = int(data[0])
S = list(data[1])
answer = 0
for i in range(N-2):
  I = i+1
  for j in range(N-1):
    J = j+1
    if J <= I:
      continue
    for k in range(N):
      K = k+1
      if K <= J:
        continue
      if J-I == K-J:
        continue
      if S[i] == S[j]:
        continue
      if S[i] == S[k]:
        continue
      if S[j] == S[k]:
        continue
      answer += 1
print(answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
n=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"]
a1 = [R,G,B]
l1=list(itertools.product(*a1))
a2 = [R,B,G]
l2=list(itertools.product(*a2))
a3 = [G,R,B]
l3=list(itertools.product(*a3))
a4 = [G,B,R]
l4=list(itertools.product(*a4))
a5 = [B,R,G]
l5=list(itertools.product(*a5))
a6 = [B,G,R]
l6=list(itertools.product(*a6))

l=l1+l2+l3+l4+l5+l6
ans=0
for i in l:
    if i[1]-i[0]!=i[2]-i[1] and i[0]<i[1]<i[2]:
        ans+=1
print (ans)

Submission status: Time Limit Exceeded

Solution code:
from numba import jit


@jit
def check(N, S):
    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] and j - i != k - j:
                    ans += 1

    return ans


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

    print(check(N, S))


if __name__ == "__main__":
    main()

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)
	if s[i] == "G":
		g.append(i)
	if s[i] == "B":
		b.append(i)
count = len(r)*len(g)*len(b)
for i in r:
	for j in g:
		if (max(i,j)+abs(i-j)) in b:
			count -= 1
		if (min(i,j)-abs(i-j)) in b:
			count -= 1
		if abs(i-j)%2==0 and abs(i-j)!=0 and ((i+j)//2) in b:
			count -= 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 0
RGB = ["R", "G", "B"]

for i in range(1, N-1):
    for j in range(i+1, N):
        if S[i-1] == S[j-1]:
            continue
        RGB = ["R", "G", "B"]
        RGB.remove(S[i-1])
        RGB.remove(S[j-1])
        count += S.count(RGB[0], j, N)
        #print(S.count(RGB[0], j, N))
        if 2 * j - i > N:
            pass
        else:
            if S[2 * j - i - 1] == RGB[0]:
                count -= 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r = []
g = []
b = []
for i in range(n):
  if s[i] == 'R':
    r.append(i)
  elif s[i] == 'G':
    g.append(i)
  else:
    b.append(i)
res = 0
if max(len(r),len(g),len(b)) == len(r):
  for i in g:
    for j in b:
      flg = 0
      if (i + j) / 2 in r:
        flg += 1
      if 2 * i - j in r:
        flg += 1
      if 2 * j - i in r:
        flg += 1
      res += len(r) - flg
else:
  for i in b:
    for j in r:
      flg = 0
      if (i + j) / 2 in g:
        flg += 1
      if 2 * i - j in g:
        flg += 1
      if 2 * j - i in g:
        flg += 1
      res += len(g) - flg
print(res)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
import collections
N=int(input())
Tr=list(input())
R=[]
G=[]
B=[]
for i in range(N):
   if Tr[i]=="R":
       R.append(i)
   if Tr[i]=="G":
       G.append(i)
   if Tr[i]=="B":
       B.append(i)
cnt=0
for i in R:
    for j in G:
        for k in B:
            if max(i,j,k)-(i+j+k-max(i,j,k)-min(i,j,k))!=(i+j+k-max(i,j,k)-min(i,j,k))-min(i,j,k):
                cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
Sl = list(S)
Nl = [int(x) for x in range(1,N+1)]
res =0
for (i,Ni) in zip(Sl,Nl):
    x = i
    Nx = Ni
    Sl2 = Sl[Nx:]
    Nl2 = Nl[Nx:]
    for (j,Nj) in zip(Sl2,Nl2):
        y = j
        if y == x:
            pass
        else:
            Ny =Nj 
            Sl3 =Sl[Ny:]
            Nl3 =Nl[Ny:]
            for (k,Nk) in zip(Sl3,Nl3):
                z = k
                if z ==y:
                    pass
                else:
                    Nz = Nk
                    Ma = Nz-Ny
                    Mb = Ny-Nx
                    judge = [x,y,z]
                    if len(judge) == len(set(judge)) :
                        if Ma != Mb:
                            res =res+1
print(res)

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:
            if 3 * (max(r, g, b) + min(r, g, b)) == 2 * (r + g + b):
                continue
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

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

	ans = 0
	r = []
	g = []
	b = []
	for i in range(3):
		if s[i] == 'R':
			r.append(i)
		elif s[i] == 'G':
			g.append(i)
		else:
			b.append(i)

	for i in range(3,n):
		if s[i] == 'R':
			r.append(i)
		elif s[i] == 'G':
			g.append(i)
		else:
			b.append(i)

		for j in range(i-2):
			if s[i] == s[j]:
				continue
			elif 'R' not in [s[i],s[j]]:
				for k in r:
					if k - j != i - k and k > j:
						ans += 1
			elif 'G' not in [s[i],s[j]]:
				for k in g:
					if k - j != i - k and k > j:
						ans += 1
			elif 'B' not in [s[i],s[j]]:
				for k in b:
					if k - j != i - k and k > j:
						ans += 1

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
N = input("")
S = input("")
N = int(N)
X = 0

for i in range(N-3):
    j = i+1
    for jj in range(N):
        if S[j] != S[i]:
            k = j+1
            while k < N:
                if S[k] != S[j] and S[k] != S[i]:
                    if j-i != k-j :
                        X += 1
                k += 1
        j += 1
        if j == N:
            break
print(X)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
L = len(S)
R = []
G = []
B = []
r = 0
g = 0
b = 0
for i in range(L):
  if S[i] == "R":
    R.append(i + 1)
    r = r + 1
  elif S[i] == "G":
    G.append(i + 1)
    g = g + 1
  elif S[i] == "B":
    B.append(i + 1)
    b = b + 1
Sum = 0
for i in range(r):
  x = R[i]
  for j in range(g):
    y = G[j]
    for k in range(b):
      z = B[k]
      if (x > y) and (y > z) and ((x - y) != (y - z)):
        Sum = Sum + 1
      elif (x > z) and (z > y) and ((x - z) != (z - y)):
        Sum = Sum + 1
      elif (y > x) and (x > z) and ((y - x) != (x - z)):
        Sum = Sum + 1
      elif (y > z) and (z > x) and ((y - z) != (z - x)):
        Sum = Sum + 1
      elif (z > x) and (x > y) and ((z - x) != (x - y)):
        Sum = Sum + 1
      elif (z > y) and (y > x) and ((z - y) != (y - x)):
        Sum = Sum + 1
print(Sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

R = [0] * N
G = [0] * N
B = [0] * N

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

cnt = 0
for i in range(N):
    sum_r = sum(R) - sum(R[:(i + 1)])
    sum_g = sum(G) - sum(G[:(i + 1)])
    sum_b = sum(B) - sum(B[:(i + 1)])
    for j in range(i + 1, N):
        sum_r -= R[j]
        sum_g -= G[j]
        sum_b -= B[j]
        if S[j] == 'R':
            if S[i] == 'B':
                cnt += sum_g
                if j + (j - i) < N:
                    cnt -= G[j + (j - i)]
            if S[i] == 'G':
                cnt += sum_b
                if j + (j - i) < N:
                    cnt -= B[j + (j - i)]
        elif S[j] == 'G':
            if S[i] == 'R':
                cnt += sum_b
                if j + (j - i) < N:
                    cnt -= B[j + (j - i)]
            if S[i] == 'B':
                cnt += sum_r
                if j + (j - i) < N:
                    cnt -= R[j + (j - i)]
        else:
            if S[i] == 'R':
                cnt += sum_g
                if j + (j - i) < N:
                    cnt -= G[j + (j - i)]
            if S[i] == 'G':
                cnt += sum_r
                if j + (j - i) < N:
                    cnt -= R[j + (j - i)]
                    
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = list(str(input()))
    print()
    sum = 0
    for i in range(0, 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:
                        sum += 1
    print(sum)



main()

Submission status: Time Limit Exceeded

Solution code:

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


for x in range(n):
    if s[x] == 'R':
        r.append(x+1)
        continue
    if s[x] == 'G':
        g.append(x+1)
        continue
    if s[x] == 'B':
        b.append(x+1)
        continue

def count(x,y,z):
    c = 0
    for i in x:
        for j in y:
            if i > j: continue 
            for k in z:
                if j > k: continue 
                if j - i != k - j:
                    c += 1
    return c

ans = 0
ans += count(r,g,b)
ans += count(r,b,g)
ans += count(b,r,g)
ans += count(b,g,r)
ans += count(g,r,b)
ans += count(g,b,r)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

dd = defaultdict(int)
rList = [0]
gList = [0]
bList = [0]
for i in range(N):
    if S[i] == "R":
        rList.append(rList[-1] + 1)
        gList.append(gList[-1])
        bList.append(bList[-1])
    elif S[i] == "G":
        rList.append(rList[-1])
        gList.append(gList[-1] + 1)
        bList.append(bList[-1])
    else:
        rList.append(rList[-1])
        gList.append(gList[-1])
        bList.append(bList[-1] + 1)

ans = 0
for i in range(N):
    for j in range(i, N):
        if S[i] == S[j]:
            continue
        tl = ["R", "G", "B"]
        tl.remove(S[i])
        tl.remove(S[j])
        target = tl[0]
        if target == "R":
            ans += rList[-1] - rList[j + 1]
        elif target == "G":
            ans += gList[-1] - gList[j + 1]
        else:
            ans += bList[-1] - bList[j + 1]

        diff = abs(i - j)
        if j + diff <= N - 1 and S[j + diff] == target:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:


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

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

Submission status: Time Limit Exceeded

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

ans = int(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 k-j == j-i:
                    continue
                elif S[i]!=S[k] and S[j]!=S[k] and k-j != j-i:
                    ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def q4():
    from itertools import combinations
    N = int(input())
    S = list(input())
    n_comb = combinations(range(N), 3)
    ans = 0
    for c in n_comb:
        if (S[c[0]] != S[c[1]] and S[c[1]] != S[c[2]] and S[c[2]] != S[c[0]] and
            c[1] - c[0] != c[2] - c[1]):
            ans += 1
    print(ans)

    
if __name__ == '__main__':
    q4()

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):
        x,y=S[i],S[j]
        if not x==y:
            for k in range(j,N):
                if not 2*j-i==k:
                    z=S[k]
                    if not (x==z or y==z):
                        ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
  N =int(input())
  S = str(input())
  A = 0
  R = []
  G = []
  B = []
  for i in range(len(S)):
    if S[i] == "R":
      R.append(i+1)
    elif S[i] == "G":
      G.append(i+1)
    else:
      B.append(i+1)
  #print(R)
  #print(G)
  #print(B)

  for r in R:
    for g in G:
      for b in B:
        C = sorted([r,g,b])
        if C[1] - C[0] != C[2] - C[1]:
          A += 1
  print(A)
main()

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin, stdout
def main():
    N=int(stdin.readline())
    S=stdin.readline().rstrip()
    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)
    cnt = 0
    R.sort()
    G.sort()
    B.sort()
    seen=set()
    for ri in R:
        for gi in G:
            for bi in B:
                if abs(gi-ri) != abs(bi-gi) and abs(ri-bi) != abs(bi-gi) and abs(ri-bi) != abs(ri-gi):
                    cnt+=1

    stdout.write(str(cnt)+"\n")

main()

Submission status: Time Limit Exceeded

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

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


for i in range(n):
  if i in r:
    for j in g:
      for k in b:
        if k < j:
          continue
        if 2*j != i+k:
          count += 1
    for j in b:
      for k in g:
        if k < j:
          continue
        if 2*j != i+k:
          count += 1      
  if i in g:
    for j in r:
      for k in b:
        if k < j:
          continue
        if 2*j != i+k:
          count += 1
    for j in b:
      for k in r:
        if k < j:
          continue
        if 2*j != i+k:
          count += 1
  if i in b:
    for j in g:
      for k in r:
        if k < j:
          continue
        if 2*j != i+k:
          count += 1
    for j in r:
      for k in g:
        if k < j:
          continue
        if 2*j != i+k:
          count += 1
  if i in r:
    r = r[1:]
  elif i in g:
    g = g[1:]
  else:
    b = b[1:]

print(count)