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 quest_d():
    N = int(input())
    S = input()

    index_dict = {
        'R':[],
        'G':[],
        'B':[]
    }
    for index in range(len(S)):
        index_dict[S[index]].append(index)

    if len(index_dict['R']) * len(index_dict['G']) * len(index_dict['B']) == 0:
        print(0)
        return

    i = 0
    sum = 0
    while i < len(S):
        a = S[i]
        j = i
        while j < len(S) - 1:
            j += 1
            if S[j] == a:
                continue

            l = 2 * j - i
            b = S[j]

            target_array = []
            for k, v in index_dict.items():
                if a != k and b != k:
                    target_array = v
                    break

            for index in target_array:
                if l != index and index > j:
                    sum += 1
        i += 1
    print(sum)

if __name__ == '__main__':
    quest_d()

Submission status: Time Limit Exceeded

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

    r_index = []
    g_index = []
    b_index = []
    for index in range(len(S)):
        if S[index] == 'R':
            r_index.append(index)
        elif S[index] == 'G':
            g_index.append(index)
        else:
            b_index.append(index)
    if len(r_index) * len(g_index) * len(b_index) == 0:
        print(0)
        return

    i = 0
    sum = 0
    while i < len(S):
        a = S[i]
        j = i
        while j < len(S) - 1:
            j += 1
            if S[j] == a:
                continue

            l = 2 * j - i
            b = S[j]
            k = j
            while k < len(S)- 1:
                k += 1
                if S[k] == a or S[k] == b or l == k:
                    continue
                sum += 1
        i += 1
    print(sum)

if __name__ == '__main__':
    quest_d()

Submission status: Time Limit Exceeded

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

    i = 0
    sum = 0
    while i < len(S):
        a = S[i]
        j = i
        while j < len(S) - 1:
            j += 1
            if S[j] == a:
                continue

            l = 2 * j - i
            b = S[j]
            k = j
            while k < len(S)- 1:
                k += 1
                if S[k] == a or S[k] == b or l == k:
                    continue
                sum += 1
        i += 1
    print(sum)

if __name__ == '__main__':
    quest_d()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from math import gcd
import sys
input = sys.stdin.readline

n = int(input())
s = input().strip()
R = [0] * (n + 1)
G = [0] * (n + 1)
B = [0] * (n + 1)
ans = 0
for i in range(len(s)):
    R[i + 1] = R[i]
    G[i + 1] = G[i]
    B[i + 1] = B[i]
    if s[i] == 'R':
        R[i + 1] += 1
    elif s[i] == 'G':
        G[i + 1] += 1
    else:
        B[i + 1] += 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'R' and s[j] == 'B':
            ans += G[j + 1] - G[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'G':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'R' and s[j] == 'G':
            ans += B[j + 1] - B[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'B':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'B' and s[j] == 'R':
            ans += G[j + 1] - G[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'G':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'B' and s[j] == 'G':
            ans += R[j + 1] - R[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'R':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'G' and s[j] == 'R':
            ans += B[j + 1] - B[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'B':
                ans -= 1
for i in range(len(s)):
    for j in range(i, len(s)):
        if s[i] == 'G' and s[j] == 'B':
            ans += R[j + 1] - R[i]
            if (i + j) % 2 == 0 and s[(i + j) // 2] == 'R':
                ans -= 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
from numba import jit

@jit
def main2(N,S):
    num = [0,0,0]

    for s in S:
        if s == "R":
            num[0] += 1
        elif s == "G":
            num[1] += 1
        else:
            num[2] += 1

    ans = num[0] * num[1] * num[2]
    sub = 0

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

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

main2(N,S)

Submission status: Time Limit Exceeded

Solution code:
from numba import jit

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

num = [0,0,0]

for s in S:
    if s == "R":
        num[0] += 1
    elif s == "G":
        num[1] += 1
    else:
        num[2] += 1

ans = num[0] * num[1] * num[2]
sub = 0

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

Submission status: Time Limit Exceeded

Solution code:
from numba import jit

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

N = int(input())
S = input()
print(search_comb(N,S))

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
n = int(input())
s = stdin.readline()
rind = []
gind = []
bind = []
ans = 0
for i in range(n):
    if s[i] == "R":
        rind.append(i+1)
    elif s[i] == "G":
        gind.append(i+1)
    else:
        bind.append(i+1)
for i in range(len(rind)):
    for j in range(len(gind)):
        for k in range(len(bind)):
            if (rind[i]+gind[j]!=2*bind[k])and(gind[j]+bind[k]!=2*rind[i])and(bind[k]+rind[i]!=2*gind[j]):
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
n = int(input())
s = stdin.readline()
rind = []
gind = []
bind = []
ans = 0
for i in range(n):
    if s[i] == "R":
        rind.append(i+1)
    elif s[i] == "G":
        gind.append(i+1)
    else:
        bind.append(i+1)
for i in range(len(rind)):
    for j in range(len(gind)):
        for k in range(len(bind)):
            ind = [rind[i],gind[j],bind[k]]
            a = sorted(ind)
            if a[0] + a[2] != 2*a[1]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

nc_lis = []
for i in enumerate(S_str,1):
    nc_lis.append(i)

count = 0
for n,c in nc_lis:
    if c == 'R':
        for n2,c2 in nc_lis:
            if c2 == 'G':
                for n3,c3 in nc_lis:
                    lis = [n,n2,n3]
                    if c3 == 'B' and sorted(lis)[-2]-min(lis) != max(lis)-sorted(lis)[-2]:
                        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
from collections import Counter
c = Counter(s)
cnt = 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[i] != s[j])&(s[j] != s[k])&(s[i] != s[k]):
                    cnt += 1  
            else:
                continue
                
print(c['R']*c['B']*c['G'] - cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = input()
cnt = 0

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

import time
import numpy as np
import math

############################################

# read data for n sequences.
temp = stdin.readline().split()
N = int(temp[0])
temp = stdin.readline().split()
data = temp[0]


r = np.sum(np.asarray([d for d in data])=="R")
g = np.sum(np.asarray([d for d in data])=="G")
b = np.sum(np.asarray([d for d in data])=="B")
out = r*g*b

if N > 2:
    for ii, i in enumerate(data[:-2]):
        for jj,j in enumerate(data[ii+1:-1]):
            jj = jj+ii+1
            if i!=j:
                sum1 = 0                    
                for d in np.asarray([d for d in "RGB"]):
                    if i!=d and j!=d:
                        let = d
                if jj+jj-ii<N:
                    if data[jj+jj-ii]==let:                    
                        out-=1
print(out)

Submission status: Time Limit Exceeded

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

############################################

# read data for n sequences.
N = int(input())
data = input()

out = 0

if N > 2:
    for ii, i in enumerate(data[:-2]):
        for jj,j in enumerate(data[ii+1:-1]):
            jj = jj+ii+1
            if i!=j:
                sum1 = 0                    
                for d in [d for d in "RGB"]:
                    if i!=d and j!=d:
                        #sum1 = np.sum(np.asarray([d for d in data[jj+1:]])==d)
                        sum1 = 0
                        for l in data[jj+1:]:
                            if d == l:
                                sum1 +=1
                        let = d
                out+=sum1
                if jj+jj-ii<N:
                    if data[jj+jj-ii]==let:                    
                        out-=1
print(out)

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = input()
cnt = 0

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

print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

from sys import stdin

import time
import numpy as np
import math



############################################

# read data for n sequences.
temp = stdin.readline().split()
N = int(temp[0])
temp = stdin.readline().split()
data = temp[0]


out = 0

if N > 2:
    for ii, i in enumerate(data[:-2]):
        for jj,j in enumerate(data[ii+1:-1]):
            jj = jj+ii+1
            if i!=j:
                sum1 = 0                    
                for d in np.asarray([d for d in "RGB"]):
                    if i!=d and j!=d:
                        sum1 = np.sum(np.asarray([d for d in data[jj+1:]])==d)
                        let = d
                out+=sum1
                if jj+jj-ii<N:
                    if data[jj+jj-ii]==let:                    
                        out-=1
print(out)

Submission status: Time Limit Exceeded

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

############################################

# read data for n sequences.
N = int(input())
data = input()

out = 0

if N > 2:
    for ii, i in enumerate(data[:-2]):
        for jj,j in enumerate(data[ii+1:-1]):
            jj = jj+ii+1
            if i!=j:
                for kk, k in enumerate(data[jj+1:]):
                    kk = kk+jj + 1
                    if i!=k and j!=k:
                        if (jj-ii) != (kk-jj):
                            out+=1
print(out)

Submission status: Time Limit Exceeded

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

from sys import stdin

import time
import numpy as np
import math


############################################

# read data for n sequences.
temp = stdin.readline().split()
N = int(temp[0])
temp = stdin.readline().split()
data = temp[0]


out = 0

if N > 2:
    for ii, i in enumerate(data[:-2]):
        for jj,j in enumerate(data[ii+1:-1]):
            jj = jj+ii+1
            if i!=j:
                for kk, k in enumerate(data[jj+1:]):
                    kk = kk+jj + 1
                    if i!=k and j!=k:
                        if (jj-ii) != (kk-jj):
                            out+=1
print(out)

Submission status: Time Limit Exceeded

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

count = 0
if(n < 3):
    print(0)
else:
    r_indexs = []
    g_indexs = []
    b_indexs = []
    for index, char in enumerate(s):
        if char == "R":
            r_indexs.append(index)
        elif char == "G":
            g_indexs.append(index)
        else:
            b_indexs.append(index)

    color_indexs = [r_indexs, g_indexs, b_indexs]

    for col_index in range(3):
        i_indexs_list = color_indexs[col_index]
        tmp_index1 = (col_index+1) % 3
        tmp_index2 = (col_index+2) % 3
        
        j_indexs_list = color_indexs[tmp_index1]
        k_indexs_list = color_indexs[tmp_index2]
        for i in i_indexs_list:
            for j in j_indexs_list:
                if i > j:
                    continue
                for k in k_indexs_list:
                    if i > k or j > k:
                        continue
                    if j-i != k-j:
                        count += 1
        
        j_indexs_list = color_indexs[tmp_index2]
        k_indexs_list = color_indexs[tmp_index1]
        for i in i_indexs_list:
            for j in j_indexs_list:
                if i > j:
                    continue
                for k in k_indexs_list:
                    if i > k or j > k:
                        continue                    
                    if 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')
c = 0
for i in range(n-1):
  for j in range(i+1, n):
    k = j*2-i
    if k >= n:
        continue
    if len(set([s[i], s[j], s[k]]))==3:
      c += 1
print(r*g*b-c)

Submission status: Time Limit Exceeded

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

count = 0
if(n < 3):
    print(1)
else:
    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):
                if s[i] == s[k] or s[j] == s[k]:
                    continue
                if j-i != k-j:
                    count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from numba import jit

@jit
def search_comb(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

N = int(input())
S = input()
print(search_comb(N,S))

Submission status: Time Limit Exceeded

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

s = np.array(list(S))
cnt = 0
for i in range(N-2):
    # make vector J
    j1 = np.array(np.ones(N)*True,dtype=bool)
    j1[:i] = False
    j1[-1:] = False
    j2 = np.where(s != s[i], True, False)  # not same color
    jj = np.array(j1 * j2, dtype=bool)

    J = np.arange(N)[jj]
    if(len(J)>0):
        for j in J:
            k1 = np.array(np.ones(N) * True, dtype=bool)
            k1[:j] = False
            k2 = np.where(s != s[j], True, False)  # not same color
            kk = j2*k1*k2
            kk = np.array(kk, dtype=bool)
            if 2*j-i<N:
                kk[2*j-i] = False
            cnt +=np.sum(kk)
print(cnt)

Submission status: Time Limit Exceeded

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

s = np.array(list(S))

cnt = 0
for i in range(N-2):
    f1 = np.ones(N) * True
    f1[:i] =  False # set False at index of less than and equal to i-1
    f2 = np.where(s != s[i], True, False) #  not same color
    ff = np.array(f1*f2,dtype=bool)
    J = np.arange(N)[ff]
    if(len(J)>0):
        for j in J:
            f3 = np.ones(N) * True
            f3[:j] = False  # set False at index of less than and equal to i-1
            f4 = f2 * np.where(s != s[i], True, False)  # not same color
            f5 = f2 * np.where(s != s[j], True, False)  # not same color
            ff = np.array(f3*f4*f5,dtype=bool)
            K = np.arange(N)[ff]
            if(len(K)>0):
                for k in K:
                    if k-j!=j-i and s[i] != s[k] and s[j] != s[k]:
                        cnt +=1
print(cnt)

Submission status: Time Limit Exceeded

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

s = np.array(list(S))
cnt = 0
for i in range(N-2):
    f1 = np.ones(N) * True
    f1[:i] =  False # set False at index of less than and equal to i-1
    f2 = np.where(s != s[i], True, False) #  not same color
    ff = np.array(f1*f2,dtype=bool)
    J = np.arange(N)[ff]
    if(len(J)>0):
        for j in J:
            f3 = np.ones(N) * True
            f3[:j] = False  # set False at index of less than and equal to i-1
            f4 = f2*np.where(s != s[j], True, False)  # not same color
            ff = np.array(f3*f4,dtype=bool)
            K = np.arange(N)[ff]
            if(len(K)>0):
                for k in K:
                    if k-j!=j-i and s[i] != s[k] and s[j] != s[k]:
                        cnt +=1
print(cnt)

Submission status: Time Limit Exceeded

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

s = np.array(list(S))
cnt = 0
for i in range(N-2):
    f1 = np.ones(N) * True
    f1[:i] =  False # set False at index of less than and equal to i-1
    f2 = np.where(s != s[i], True, False) #  not same color
    ff = np.array(f1*f2,dtype=bool)
    J = np.arange(N)[ff]
    for j in J:
        f3 = np.ones(N) * True
        f3[:j] = False  # set False at index of less than and equal to i-1
        f4 = f2*np.where(s != s[j], True, False)  # not same color
        ff = np.array(f3*f4,dtype=bool)
        K = np.arange(N)[ff]
        for k in K:
            if k-j!=j-i and s[i] != s[k] and s[j] != s[k]:
                cnt +=1
print(cnt)

Submission status: Time Limit Exceeded

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

s = np.array(list(S))
cnt = 0
for i in range(N-2):
    f1 = np.ones(N) * True
    f1[:i] =  False # set False at index of less than and equal to i-1
    f2 = np.where(s != s[i], True, False) #  not same color
    ff = np.array(f1*f2,dtype=bool)
    J = np.arange(N)[ff]
    for j in J:
        f3 = np.ones(N) * True
        f3[:j] = False  # set False at index of less than and equal to i-1
        f4 = f2*np.where(s != s[j], True, False)  # not same color
        ff = np.array(f3*f4,dtype=bool)
        K = np.arange(N)[ff]
        for k in range(j,N):
            if k-j!=j-i and s[i] != s[k] and s[j] != s[k]:
                cnt +=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = str(input())
s = list(S)
N = len(s)
f=[]
for i in range(N):
    f.append(False)
cnt = 0
f = np.array(f)
for i in range(N-2):
    f=f*False
    for j in range(i+1, N-1):
        f[j] = s[i] != s[j]
    J = np.arange(N)[f]
    for j in J:
        for k in range(j,N):
            if k-j!=j-i and s[i] != s[k] and s[j] != s[k]:
                cnt +=1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
N=int(input())
S=list(input())
n=0
for i, j, k in product(range(1,N+1),repeat=3):
  if j-i!=k-j and 1<=i and i<j and j<k <=N and S[i-1]!=S[j-1] and S[j-1]!=S[k-1] and S[k-1]!=S[i-1]:
    n+=1
print(n)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
a=[0]*4

for i in range(N):
  if S[i]=='R':a[0]+=1
  if S[i]=='G':a[1]+=1
  if S[i]=='B':a[2]+=1
    
for i in range(N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i] and j-i==k-j:
        a[3]+=1

print(a[0]*a[1]*a[2]-a[3])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
count = 0

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
count = 0

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

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def func(S,i,j,k):
    b = True
    if (j-i) == (k-j):
        b=False
        return b
    if (S[i-1] == S[j-1]):
        b = False
        return b
    if (S[i-1] == S[k-1]):
        b = False
        return b
    if (S[j-1] == S[k-1]):
        b = False
        return b

    return b

N = int(input())
S = str(input())
#N=39
#S ="RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
import numpy as np

cnt = 0
for i in range(1,N+1):
    for j in range(i+1, N + 1):
        for k in range(j+1, N + 1):
            if func(S,i,j,k):
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def func(S,i,j,k):
    b = True
    if (j-i) == (k-j):
        b=False
    if (S[i-1] == S[j-1]):
        b = False
    if (S[i-1] == S[k-1]):
        b = False
    if (S[j-1] == S[k-1]):
        b = False

    return b

N = int(input())
S = str(input())
#N=39
#S ="RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB"
import numpy as np

cnt = 0
for i in range(1,N+1):
    for j in range(i+1, N + 1):
        for k in range(j+1, N + 1):
            if func(S,i,j,k):
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
input()
str = input()
lst_R = []
lst_G = []
lst_B = []
ans = 0


for i in range(len(str)):
    if str[i] == 'R':
        lst_R.append(i)

    elif str[i] == 'G':
        lst_G.append(i)

    else:
        lst_B.append(i)

def core():
    global ans
    for i in range(len(all[0])):

        for j in range(len(all[1])):
            if all[1][j] < all[0][i]:                   # condition is broken
                continue

            for k in range(len(all[2])):
                if all[2][k] < all[1][j]:
                    continue
                else:
                    if all[2][k] - all[1][j] != all[1][j] - all[0][i]:
                        ans += 1

all = [lst_R, lst_G, lst_B]

def comparison(lst1, lst2):                #lst1 index < lst2 index
    for i in range(len(lst1)):
        for j in range(len(lst2)):
            if lst2[j]>lst1[i]:
                return False
    return True#lst1>lst2



def swap(i):
    if i != 2:
        all[0], all[2] = all[2], all[0]

for i in range(3):      # len(all)

    if comparison(all[0], all[1])== True or comparison(all[0], all[2])== True:
        swap(i)
        continue

    # iterate process in order

    core()

    if comparison(all[1], all[2])== True:
        swap(i)
        continue

    all[1], all[2] = all[2], all[1]  # swap list order

    # iterate process in order
    core()

    swap(i)

print(ans)

Submission status: Time Limit Exceeded

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

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

print(count)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(0, n-2):
    for j in range(i+1,n-1):
        if s[i] == s[j]:
            count += 0
        else:
            for k in range(j+1,n):
                if s[i] == s[k] or s[j] == s[k]:
                    count +=0
                elif j-i == k-j:
                    count +=0
                else:
                    count +=1

print(count)

Submission status: Time Limit Exceeded

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

    # su = 0
    # for i, si in enumerate(S):
    #     for j, sj in enumerate(S[i+1:]):
    #         if si == sj:
    #             continue
    #         for k, sk in enumerate(S[i+j+2:]):
    #             # print(si, sj, sk)
    #             # print(i+1, j+i+2, k+j+i+2)
    #             if j == k:
    #                 continue
    #             elif sj != sk and si != sk:
    #                 su += 1
    # print(su)

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

ct = 0
for i in range(n-2):
    rgb = [s[i]]
    for j in range(i+1,n-1):
        if rgb[0] != s[j]:
            rgb.append(s[j])
            for k in range(j+1,n):
                if rgb[0] != s[k] and rgb[1] != s[k]:
                    if j-i != k-j:
                        ct += 1
            rgb = [s[i]]
print(ct)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
import itertools
n=int(input())
s=str(input())
R=[]
B=[]
G=[]
for i in range(n):
  if s[i]=='R':
    R.append(i)
  elif s[i]=='B':
    B.append(i)
  else:
    G.append(i)
ans=len(R)*len(B)*len(G)
l=itertools.permutations([R,B,G])
for r,b,g in l:
  for i in r:
    index=bisect.bisect_left(b,i)
    for j in b[index:]:
      index2=bisect.bisect_left(g,j)
      if j+j-i in g[index2:]:
        ans-=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import itertools
n=int(input())
s=str(input())
R=[]
B=[]
G=[]
for i in range(n):
  if s[i]=='R':
    R.append(i)
  elif s[i]=='B':
    B.append(i)
  else:
    G.append(i)
ans=0
l=itertools.permutations([R,B,G])
for r,b,g in l:
  V=len(g)
  for i in r:
    index=bisect.bisect_left(b,i)
    for j in b[index:]:
      index2=bisect.bisect_left(g,j)
      tmp=V-index2
      if j+j-i in g[index2:]:
        tmp-=1
      ans+=tmp
print(ans)

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):
        for k in range(j + 1, n):
            if (s[i] != s[j]):
                if (s[i] != s[k]):
                    if (s[j] != s[k]):
                        if ((j - i) != (k - j)):
                            #print(s[i] + s[j] + s[k])
                            #print(str(i) + str(j) + str(k))
                            ans += 1
                        
print(ans)

Submission status: Time Limit Exceeded

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

N = int(input())
I = input()

R = I.count('R')
G = I.count('G')
B = I.count('B')

sum = R * G * B

for i in range(N):
    for j in range(i+1,N):
        if I[i] != I[j]:
            k = j + j - i
            if k >= N:
                continue
            if I[i] != I[k] and I[j] != I[k]:
                sum -= 1

print(sum)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())
I = input()

R = I.count('R')
G = I.count('G')
B = I.count('B')

sum = R * G * B

for i in range(N):
    for j in range(i+1,N):
        if I[i] != I[j]:
            k = j + j - i
            if k >= N:
                continue
            if I[i] != I[k] and I[j] != I[k]:
                sum -= 1

print(sum)