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 sys
input = sys.stdin.readline
#n = int(input())
#l = list(map(int, input().split()))
'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''
n=int(input())
s=input().rstrip('\n')

from collections import Counter
a=Counter(s)
#print(a)
num=a["R"]*a["G"]*a["B"]
#print(num)
for i in range(n-2):
    for j in range(1,n):
        if i+2*j<n:
            #print([i,i+j,i+2*j,s[i], s[i+j], s[i+2*j]])
            if set([s[i],s[i+j],s[i+2*j]])==set(["R","G","B"]):
                num-=1
print(num)

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

print(cnt)

Submission status: Time Limit Exceeded

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

cnt=s.count('R')*s.count('G')*s.count('B')
for i in range(n-2):
    for j in range(i+1,n-1):
        k=2*j-i
        if j<k<n:
            if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
                cnt-=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = S.count('R') * S.count('G') * S.count('B')
for i in range(N):
  for j in range(i, N):
    k = 2*j - i
    if k >= N:
      break
    if len(set([S[i], S[j],S[k]])) ==3:
      ans -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# i, j, k

# N * (N - 1) * (N - 2)
# 4000^3

# 4
# RRGB

# B (k=4)
# G (j=3)
# R (i=1)

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

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

    return count

def resolve_v2(N, S):
    count = 0

    for i in range(N):
        for j in range(i+1, N):
            if S[i] == S[j]:
                continue

            for k in range(j+1, N):
                if S[i] == S[k] or S[j] == S[k] or k-j == j-i:
                    continue

                count += 1

    return count

if __name__ == '__main__':
    
    count = resolve_v2(N, S)
    print(count)

Submission status: Time Limit Exceeded

Solution code:
# i, j, k

# N * (N - 1) * (N - 2)
# 4000^3

# 4
# RRGB

# B (k=4)
# G (j=3)
# R (i=1)

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

count = 0

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


print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r_count = s.count('R')
g_count = s.count('G')
b_count = s.count('B')

count=0
for i in range(n):
    a = s[i]
    for j in range(i+1,n):
        if (2*j-i)>(n-1):
            continue
        b=s[j]
        c=s[2*j-i]
        if a!=b and b!=c and a!=c:
            count+=1
print(r_count*g_count*b_count-count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
r_count = s.count('R')
g_count = s.count('G')
b_count = s.count('B')

count=0
for i in range(n):
    for j in range(i+1,n):
        if (2*j-i)>(n-1):
            continue
        if s[i]!=s[j]!=s[2*j-i] and s[j]!=s[2*j-i] and s[i]!=s[2*j-i]:
            count+=1
print(r_count*g_count*b_count-count)

Submission status: Time Limit Exceeded

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

ans=0
for j in range(1,n-1):
    for i in range(j):
        for k in range(j+1,n):
            if (j-i)!=(k-j) and 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 = list(input())
r = []
g = []
b = []
for i in range(n):
    if s[i] == 'R':
        r.append(i)
    elif s[i] == 'G':
        g.append(i)
    else:
        b.append(i)
ans = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        for k in b:
            if i*2==(j+k)  or j*2==(k+i) or k*2==(i+j):
                ans -=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
r = []
g = []
b = []
for i in range(n):
    if s[i] == 'R':
        r.append(i)
    elif s[i] == 'G':
        g.append(i)
    else:
        b.append(i)
ans = len(r)*len(g)*len(b)
for i in r:
    for j in g:
        for k in b:
            if float(k) == (i+j)/2 or float(i) == (j+k)/2 or float(j) == (k+i)/2:
                ans -=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def solve2():
    n = int(input())
    s = list(input())
    ans = 0
    for i in range(n):
        if s[i] == "R":
            for j in range(n):
                if s[j] == "G":
                    for k in range(n):
                        if s[k] == "B":
                            if float(k) == (i+j)/2 or float(i) == (j+k)/2 or float(j) == (k+i)/2:
                                pass
                            else:
                                ans += 1
    return ans
print(solve2())

Submission status: Time Limit Exceeded

Solution code:
if __name__ == "__main__":
    N = int(input())
    S = input()

    # (1) Si≠Sj & Sj≠Sk & Sk≠Si ← すべて違う文字(RGBを網羅している)ということ
    # (2) j-i≠k-j ← 等間隔でないということ 

    R = []
    G = []
    B = []
    for idx, s in enumerate(S):
        if s == "R":
            R.append(idx)
        elif s == "G":
            G.append(idx)
        else:
            B.append(idx)
    
    total = 0
    for r in R:
        for g in G:
            for b in B:
                rgb = sorted([r, g, b])
                total += rgb[1] - rgb[0] != rgb[2] - rgb[1]
    
    print(total)

Submission status: Time Limit Exceeded

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

r=g=b=0

for idx, s in enumerate(string):
    if s == 'R':
        r+=1
    elif s == 'B':
        b+=1
    elif s == 'G':
        g+=1

ans = r*g*b

for i in range(n):
    for j in range(i+1, n):
        k = 2*j - i
        if k < n and len(set([string[i],string[j], string[k]])) == 3:
            ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

# r=g=b=0

# for idx, s in enumerate(string):
#     if s == 'R':
#         r+=1
#     elif s == 'B':
#         b+=1
#     elif s == 'G':
#         g+=1

ans = string.count('R')*string.count('G')*string.count('B')

for i in range(n):
    for k in range(i+1, n):
        if string[i] != string[k]:
            if (k+i)%2 == 0 and len(set([string[i], string[k], string[(k+i)//2]])) ==3:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

r=g=b=0

for idx, s in enumerate(string):
    if s == 'R':
        r+=1
    elif s == 'B':
        b+=1
    elif s == 'G':
        g+=1

ans = r*g*b

for i in range(n):
    for k in range(i+1, n):
        if string[i] != string[k]:
            if (k+i)%2 == 0 and len(set([string[i], string[k], string[(k+i)//2]])) ==3:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

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

r=g=b=0

for idx, s in enumerate(string):
    if s == 'R':
        r+=1
    elif s == 'B':
        b+=1
    elif s == 'G':
        g+=1

ans = r*g*b

for i in range(n):
    for k in range(i+1, n):
        rgb = set(['R','G','B'])
        if string[i] != string[k]:
            rgb.remove(string[i])
            rgb.remove(string[k])
            if (k+i)%2 == 0 and string[(k+i)//2] in rgb:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
s = str(input())
r_list = []
g_list = []
b_list = []
count = 0
for i in range(len(s)):
    if s[i] == "R":
        r_list.append(i)
    elif s[i] == "G":
        g_list.append(i)
    else:
        b_list.append(i)

for r in r_list:
    for g in g_list:
        x = abs(r-g)
        if max(r,g)+x in b_list:
            count += 1
        if min(r,g)-x in b_list:
            count += 1
        if(r+g)/2 in b_list:
            count += 1
print(len(r_list)*len(g_list)*len(b_list)-count)

Submission status: Time Limit Exceeded

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

def main():
    if(n<4):
        return 0
    ans=0
    for i in range(n):
        for j in range(i+1,n):
            nk = j-i
            for k in range(j+1,n):
                
                if((k-j)==nk):
                    continue
                if(s[i]==s[j] or s[j]==s[k] or s[i]==s[k]):
                    continue
                ans += 1
    return ans
print(main())

Submission status: Time Limit Exceeded

Solution code:

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

r = []
g = []
b = []
for i, c in enumerate(s):
    if 'R' == c:
        r.append(i + 1)
    elif 'G' == c:
        g.append(i + 1)
    else:
        b.append(i + 1)

ans = 0
for i in r:
    for j in g:
        for k in b:
            if i <= j <= k:
                if j - i != k -j:
                    ans += 1
            if i <= k <= j:
                if j - k != k - i:
                    ans += 1
            if j <= i <= k:
                if k -i != i - j :
                    ans += 1
            if j <= k <= i:
                if i - k != k - j:
                    ans += 1
            if k <= i <= j:
                if j-i != i - k:
                    ans += 1
            if k <= j <= i:
                if i-j != j -k:
                    ans += 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
s = input()
s = s.replace('R','0')
s = s.replace('G','1')
s = s.replace('B','2')
s = s.replace('\n','')
s = tuple(map(int,s))
rgb = [[0 for _ in range(3)] for _ in range(n+1)]
for i in range(n):
    for j in range(3):
        rgb[i+1][j] = rgb[i][j]
    rgb[i+1][s[i]] = rgb[i][s[i]] +1
rgb = tuple(rgb)
ans = 0
for i in range(n):
    a = s[i]
    for j in range(i+1,n):
        b = s[j]
        if a != b:
            c = 3 -a- b
            ans += rgb[n][c] - rgb[j+1][c]
            if 2*j-i < n and s[2*j-i] == c:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s = s.replace('R','0')
s = s.replace('G','1')
s = s.replace('B','2')
s = tuple(map(int,s))
rgb = [[0 for _ in range(3)] for _ in range(n+1)]
for i in range(n):
    for j in range(3):
        rgb[i+1][j] = rgb[i][j]
    rgb[i+1][s[i]] = rgb[i][s[i]] +1
rgb = tuple(rgb)
ans = 0
for i in range(n):
    a = s[i]
    for j in range(i+1,n):
        b = s[j]
        if a != b:
            c = 3 -a- b
            ans += rgb[n][c] - rgb[j+1][c]
            if 2*j-i < n and s[2*j-i] == c:
                ans -= 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s = s.replace('R','0')
s = s.replace('G','1')
s = s.replace('B','2')
s = tuple(map(int,s))
rgb = [[0 for _ in range(3)] for _ in range(n+1)]
for i in range(n):
    for j in range(3):
        rgb[i+1][j] = rgb[i][j]
    rgb[i+1][s[i]] = rgb[i][s[i]] +1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s = s.replace('R','0')
s = s.replace('G','1')
s = s.replace('B','2')
s = list(map(int,s))
rgb = [[0 for _ in range(3)] for _ in range(n+1)]
for i in range(n):
    for j in range(3):
        rgb[i+1][j] = rgb[i][j]
    rgb[i+1][s[i]] = rgb[i][s[i]] +1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3

from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

import copy
N = int(input())
S = input()

ct = 0

def dfs(index, RGB, indexs):
    global ct
    if RGB[S[index]] == 1:
        return
    else:
        RGB[S[index]] = 1
        indexs += [index]
        
        if len(indexs) == 2:

            for key, value in RGB.items():
                if value == 0:
                    ct += Counter(S[index+1:])[key]
                    h = indexs[1]+indexs[1] - indexs[0] +1
                    if 0 < h <= N and S[h-1] == key:
                        ct -= 1

                    return

        for i in range(index+1, N):
            dfs(i, RGB.copy(), copy.copy(indexs))

for i in range(N):
    dfs(i, {"R":0, "G":0, "B":0}, [])

print(ct)

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3

from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

import copy
N = int(input())
S = input()

hoge = ["R", "G", "B"]
ct = 0

def dfs(index, RGB, indexs):
    global ct
    if RGB[S[index]] == 1:
        return
    else:
        RGB[S[index]] = 1
        indexs += [index]
        
        if len(indexs) == 2:

            for key, value in RGB.items():
                if value == 0:
                    ct += Counter(S[index+1:])[key]
                    h = indexs[1]+indexs[1] - indexs[0] +1
                    if 0 < h <= N and S[h-1] == key:
                        ct -= 1

                    return

        for i in range(index+1, N):
            dfs(i, RGB.copy(), copy.copy(indexs))

for i in range(N):
    dfs(i, {"R":0, "G":0, "B":0}, [])

print(ct)

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3

from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

import copy
N = int(input())
S = input()

hoge = ["R", "G", "B"]
ct = 0

def dfs(index, RGB, indexs):
    global ct
    if S[index] in RGB:
        return
    else:
        RGB += [S[index]]
        indexs += [index]
        
        if len(RGB) == 2:

            for i in hoge:
                if not i in RGB:
                    ct += list(S[index+1:]).count(i)
                    h = indexs[1]+indexs[1]+2 - (indexs[0]+1)

                    if 0 < h <= N and S[h-1] == i:
                        ct -= 1
                    
                    return

        for i in range(index+1, N):
            dfs(i, copy.copy(RGB), copy.copy(indexs))

for i in range(N):
    dfs(i, [], [])

print(ct)

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3

from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

import copy
N = int(input())
S = input()

hoge = ["R", "G", "B"]
ct = 0

def dfs(index, RGB, indexs):
    global ct
    if S[index] in RGB:
        return
    else:
        RGB += [S[index]]
        indexs += [index]
        
        if len(RGB) == 2:

            for i in hoge:
                if not i in RGB:
                    ct += list(S[index+1:]).count(i)
                    h = indexs[1]+indexs[1]+2 - (indexs[0]+1)

                    if 0 < h <= N and S[h-1] == i:
                        ct -= 1
                    
                    return

        for i in range(index+1, N):
            dfs(i, copy.copy(RGB), copy.copy(indexs))

for i in range(N):
    dfs(i, [], [])

print(ct)

Submission status: Time Limit Exceeded

Solution code:
#! /usr/bin/env python3

from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

import copy
N = int(input())
S = input()

ct = 0

def dfs(index, RGB, indexs):
    global ct
    if S[index] in RGB:
        return
    else:
        RGB += [S[index]]
        indexs += [index]
        
        if len(RGB) == 3 and (indexs[1] - indexs[0] != indexs[2] - indexs[1]):
            ct += 1
            return

        for i in range(index+1, N):
            dfs(i, copy.copy(RGB), copy.copy(indexs))

for i in range(N):
    dfs(i, [], [])

print(ct)

Submission status: Time Limit Exceeded

Solution code:
import copy

n = int(input())
s = str(input())
r_list = []
g_list = []
b_list = []
count = 0
for i in range(len(s)):
    if s[i] == "R":
        r_list.append(i)
    elif s[i] == "G":
        g_list.append(i)
    else:
        b_list.append(i)

for r in r_list:
    for g in g_list:
        x = abs(r-g)
        b_list_copy = copy.copy(b_list)
        try:
            b_list_copy.remove(max(r,g)+x)
        except ValueError:
            pass
        try:
            b_list_copy.remove(min(r,g)-x)
        except ValueError:
            pass
        try:
            b_list_copy.remove((r+g)/2)
        except ValueError:
            pass
        count += len(b_list_copy)
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
List = list(input())
ListR = []
ListG = []
ListB = []

for i in range(N):
  if List[i]=="R":
    ListR.append(i)
  elif List[i]=="G":
    ListG.append(i)
  else:
    ListB.append(i)
    
ans = len(ListR)*len(ListG)*len(ListB)
for a in ListR:
  for i in range(1,max([a//2, 2000-a//2, min([a,4000-a])])):
    if a+i in ListG:
      if a+2*i in ListB:
        ans -=1
      if a-i in ListB:
        ans -=1
    if a-i in ListG:
      if a-2*i in ListB:
        ans -=1
      if a+i in ListB:
        ans -=1
    if a+2*i in ListG:
      if a+i in ListB:
        ans -=1
    if a-2*i in ListG:
      if a-i in ListB:
        ans -=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
List = list(input())
ListR = []
ListG = []
ListB = []

for i in range(N):
  if List[i]=="R":
    ListR.append(i)
  elif List[i]=="G":
    ListG.append(i)
  else:
    ListB.append(i)
    
ans = len(ListR)*len(ListG)*len(ListB)
for a in ListR:
  for i in range(1,2000):
    if a+i in ListG:
      if a+2*i in ListB:
        ans -=1
      if a-i in ListB:
        ans -=1
    if a-i in ListG:
      if a-2*i in ListB:
        ans -=1
      if a+i in ListB:
        ans -=1
    if a+2*i in ListG:
      if a+i in ListB:
        ans -=1
    if a-2*i in ListG:
      if a-i in ListB:
        ans -=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
List = list(input())
ListR = []
ListG = []
ListB = []

for i in range(N):
  if List[i]=="R":
    ListR.append(i)
  elif List[i]=="G":
    ListG.append(i)
  else:
    ListB.append(i)
    
ans = len(ListR)*len(ListG)*len(ListB)
for a in ListR:
  for b in ListG:
    for c in ListB:
      L=[a,b,c]
      L.sort()
      if L[1]-L[0] == L[2]-L[1]:
        ans -=1
print(ans)

Submission status: Time Limit Exceeded

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

r = len([i for i in range(n) if s[i] == "R"])
g = len([i for i in range(n) if s[i] == "G"])
b = len([i for i in range(n) if s[i] == "B"])

count = 0
for i in range(n):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k >= n:
            break
        if all([s[i] != s[j], s[j] != s[k], s[k] != s[i]]):
            count += 1

print(r * g * b - count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=input()
dictR=[]
dictG=[]
dictB=[]
r=0
g=0
b=0
r=S.count("R")
g=S.count("G")
b=S.count("B")
sum=r*g*b
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            si=S[i]
            sj=S[j]
            sk=S[k]
            if k-j!=j-i or (si==sj or sj==sk or si==sk):
                sum+=1



print(sum-(n*(n-1)*(n-2)//6))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=input()
dictR=[]
dictG=[]
dictB=[]
r=0
g=0
b=0
r=S.count("R")
g=S.count("G")
b=S.count("B")
sum=r*g*b
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            si=S[i]
            sj=S[j]
            sk=S[k]
            if k-j==j-i and(si!=sj and sj!=sk and si!=sk):
                sum-=1



print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
R = [i for i in range(n) if s[i] == "R"]
G = [i for i in range(n) if s[i] == "G"]
B = [i for i in range(n) if s[i] == "B"]
count = 0
for r in R:
    for g in G:
        for b in B:
            pos = sorted([r, g, b])
            if pos[1] - pos[0] != pos[2] - pos[1]:
                count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=input()
dictR=[]
dictG=[]
dictB=[]
r=0
g=0
b=0
r=S.count("R")
g=S.count("G")
b=S.count("B")
sum=r*g*b
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if k-j==j-i and(S[i]!=S[j] and S[j]!=S[k] and S[i]!=S[k]):
                sum-=1



print(sum)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=input()
dictR=[]
dictG=[]
dictB=[]
r=0
g=0
b=0
for i in range(len(S)):
    if S[i]=="R":
        dictR.append(i)
        r+=1
    elif S[i]=="G":
        dictG.append(i)
        g+=1
    else:
        dictB.append(i)
        b+=1
sum=r*g*b
if sum!=0:
    for i in dictR:
        for j in dictG:
            for k in dictB:
                maxi=max(i,max(j,k))
                mini=min(i,min(j,k))
                medi=i+j+k-maxi-mini
                if maxi-medi==medi-mini:
                    sum-=1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
S=input()
dictR=[]
dictG=[]
dictB=[]
for i in range(len(S)):
    if S[i]=="R":
        dictR.append(i)
    elif S[i]=="G":
        dictG.append(i)
    else:
        dictB.append(i)
sum=len(dictR)*len(dictG)*len(dictB)
if sum!=0:
    for i in dictR:
        for j in dictG:
            for k in dictB:
                maxi=max(i,max(j,k))
                mini=min(i,min(j,k))
                medi=i+j+k-maxi-mini
                if maxi-medi==medi-mini:
                    sum-=1

print(sum)

Submission status: Time Limit Exceeded

Solution code:
def numS(S):
    x=len(S)
    if x<4:
        return 0
    T=S[1:]
   # print(T)
    l=x-1
    sum=0
    now=S[0]
    for i in range(l):
        now2=T[i]
    #    print(now2)
        if now2!=now:
            for j in range(i+1,l):
                now3=T[j]
     #           print(now,now2,now3)
                if now3!=now2 and now3!=now:
      #              print(i,j)
                    sum+=1
                    if 2*i+1==j:
                        sum-=1
    return numS(T)+sum
n=int(input())
S=input()
print(numS(S))

Submission status: Time Limit Exceeded

Solution code:
def numS(S):
    if len(S)<4:
        return 0
    T=S[1:]
   # print(T)
    sum=0
    now=S[0]
    for i in range(len(T)):
        now2=T[i]
    #    print(now2)
        if now2!=now:
            for j in range(i+1,len(T)):
                now3=T[j]
     #           print(now,now2,now3)
                if now3!=now2 and now3!=now:
      #              print(i,j)
                    sum+=1
                    if 2*i+1==j:
                        sum-=1
    return numS(T)+sum
n=int(input())
S=input()
print(numS(S))

Submission status: Time Limit Exceeded

Solution code:
import itertools
import sys
sys.setrecursionlimit(10**7)
input = lambda: sys.stdin.readline().strip()

def main():
    N = int(input())
    S = input()
    ijk_list = list(itertools.combinations([i for i in range(1,N+1)],3))
    ans = 0
    for i,j,k in ijk_list:
        i,j,k = i-1,j-1,k-1
        if (S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]) and (j-i)!=(k-j):
            ans += 1
    print(ans)

main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

r = 0
g = 0
b = 0
cnt_S = []
for i, s in enumerate(S):
    if s == 'R':
        r += 1
    elif s == 'G':
        g += 1
    else:
        b += 1
    cnt_S.append([r, g, b])

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == 'R':
            if S[j] == 'G':
                ans += cnt_S[-1][2] - cnt_S[j][2]
                if 2 * j - i < N and S[j + j - i] == 'B':
                    ans -= 1
            if S[j] == 'B':
                ans += cnt_S[-1][1] - cnt_S[j][1]
                if 2 * j - i < N and S[j + j - i] == 'G':
                    ans -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                ans += cnt_S[-1][2] - cnt_S[j][2]
                if 2 * j - i < N and S[j + j - i] == 'B':
                    ans -= 1
            if S[j] == 'B':
                ans += cnt_S[-1][0] - cnt_S[j][0]
                if 2 * j - i < N and S[j + j - i] == 'R':
                    ans -= 1
        else:
            if S[j] == 'R':
                ans += cnt_S[-1][1] - cnt_S[j][1]
                if 2 * j - i < N and S[j + j - i] == 'G':
                    ans -= 1
            if S[j] == 'G':
                ans += cnt_S[-1][0] - cnt_S[j][0]
                if 2 * j - i < N and S[j + j - i] == 'R':
                    ans -= 1
print(ans)

Submission status: Time Limit Exceeded

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

Rs=set()
Gs=set()
Bs=set()

for i in range(len(S)):
    if S[i]=="R":
        Rs.add(i)
    elif S[i]=="G":
        Gs.add(i)
    else:
        Bs.add(i)

def solve(Rs,Gs,Bs):
    cnt=0
    for r in Rs:
        for g in Gs:
            NGs={(r+g)/2,max(r,g)+abs(r-g),min(r,g)-abs(r-g)}
            ansSet=Bs.difference(NGs)
            cnt+=len(ansSet)
    return(cnt)

if min(len(Rs),len(Gs),len(Bs)) == 0:
    print(0)
else:
    print(solve(Rs,Gs,Bs))

Submission status: Time Limit Exceeded

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

Rs=[]
Gs=[]
Bs=[]

for i in range(len(S)):
    if S[i]=="R":
        Rs.append(i)
    elif S[i]=="G":
        Gs.append(i)
    else:
        Bs.append(i)

def solve(Rs,Gs,Bs):
    cnt=0
    for r in Rs:
        for g in Gs:
            for b in Bs:
                if abs(r-g) == abs(r-b) or abs(r-g)==abs(g-b) or abs(r-b)==abs(g-b):
                    pass
                    #print(r,g,b)
                else:
                    cnt+=1
    return(cnt)

if min(len(Rs),len(Gs),len(Bs)) == 0:
    print(0)
else:
    print(solve(Rs,Gs,Bs))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Wed May  6 02:13:01 2020

@author: shinba
"""

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

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

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

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

for i in R:
    for j in G:
        if 2*j - i in B:
            ans -= 1
        if (i+j)%2 == 0:
            if (i+j)//2 in B:
                ans -= 1
        if 2*i - j in B:
            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[i] = None
    elif S[i] == 'G':
        G[i] = None
    else:
        B[i] = None

#print(R, G, B)

cnt = 0

for r in R:
    for g in G:
        for b in B:
            if (r * 2 == g + b) or (g * 2 == r + b) or (b * 2 == r + g):
                continue
            cnt += 1
            
print(cnt)

Submission status: Time Limit Exceeded

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

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

all = int(N*(N-1)*(N-2)/6)
print(all-counter)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
l_r = []
l_g = []
l_b = []
counter = 0
for i in range(n):
  if s[i] == "R":
    l_r.append(i)
  elif s[i] == "G":
    l_g.append(i)
  else:
    l_b.append(i)
for i in l_r:
  for j in l_g:
    for k in l_b:
      if abs(i - j) != abs(i - k) and abs(i - j) != abs(j - k) and abs(i - k) != abs(j - k):
          counter += 1
print(counter)

Submission status: Time Limit Exceeded

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

counter = 0
for k in range(N):
    for j in range(k):
        if S[k]==S[j]: 
            continue
        else:       
            for i in range(j):
                if  (not S[i]==S[k]) and (not S[j]==S[i]) and (not(j-i==k-j)):
                    counter += 1
                
print(counter)

Submission status: Time Limit Exceeded

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

counter = 0
for k in range(N):
    for j in range(k):
        for i in range(j):
            if (not S[i]==S[j]) and (not S[i]==S[k]) and (not S[j]==S[k]) and not(j-i==k-j):
                counter += 1
print(counter)

Submission status: Time Limit Exceeded

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