import itertools

import bisect



N = int(eval(input()))

S = eval(input())

R_set = set()

G_set = set()

B_set = set()

R = []

G = []

B = []

R_ids = [0] * N

G_ids = [0] * N

B_ids = [0] * N



for i, s in enumerate(S):

    if s == 'R':

        R_set.add(i)

        R.append(i)

        R_ids[i] = 1

    elif s == 'G':

        G_set.add(i)

        G.append(i)

        G_ids[i] = 1

    else:

        B_set.add(i)

        B.append(i)

        B_ids[i] = 1





R_ids = list(itertools.accumulate(reversed(R_ids)))[::-1]

G_ids = list(itertools.accumulate(reversed(G_ids)))[::-1]

B_ids = list(itertools.accumulate(reversed(B_ids)))[::-1]



ans = 0

for r_idx in R:

    start = bisect.bisect_left(G, r_idx)

    for g_idx in G[start:]:

        diff = g_idx - r_idx

        num = B_ids[g_idx]

        if g_idx+diff in B_set:

            num -= 1

        ans += num

    

    start = bisect.bisect_left(B, r_idx)

    for b_idx in B[start:]:

        diff = b_idx - r_idx

        num = G_ids[b_idx]

        if b_idx+diff in G_set:

            num -= 1

        ans += num



for g_idx in G:

    start = bisect.bisect_left(R, g_idx)

    for r_idx in R[start:]:

        diff = r_idx - g_idx

        num = B_ids[r_idx]

        if r_idx+diff in B_set:

            num -= 1

        ans += num

    

    start = bisect.bisect_left(B, g_idx)

    for b_idx in B[start:]:

        diff = b_idx - g_idx

        num = R_ids[b_idx]

        if b_idx+diff in R_set:

            num -= 1

        ans += num



for b_idx in B:

    start = bisect.bisect_left(G, b_idx)

    for g_idx in G[start:]:

        diff = g_idx - b_idx

        num = R_ids[g_idx]

        if g_idx+diff in R_set:

            num -= 1

        ans += num

    

    start = bisect.bisect_left(R, b_idx)

    for r_idx in R[start:]:

        diff = r_idx - b_idx

        num = G_ids[r_idx]

        if r_idx+diff in G_set:

            num -= 1

        ans += num





print(ans)
import itertools

import bisect



N = int(eval(input()))

S = eval(input())

R_set = set()

G_set = set()

B_set = set()

R = []

G = []

B = []

R_ids = [0] * N

G_ids = [0] * N

B_ids = [0] * N



for i, s in enumerate(S):

    if s == 'R':

        R_set.add(i)

        R.append(i)

        R_ids[i] = 1

    elif s == 'G':

        G_set.add(i)

        G.append(i)

        G_ids[i] = 1

    else:

        B_set.add(i)

        B.append(i)

        B_ids[i] = 1





R_ids = list(itertools.accumulate(reversed(R_ids)))[::-1]

G_ids = list(itertools.accumulate(reversed(G_ids)))[::-1]

B_ids = list(itertools.accumulate(reversed(B_ids)))[::-1]



r = (R_set, R, R_ids)

g = (G_set, G, G_ids)

b = (B_set, B, B_ids)



def count_rgb(r, g, b):

    res = 0

    r_set, _r, r_ids = r

    g_set, _g, g_ids = g

    b_set, _b, b_ids = b

    for r_idx in _r:

        start = bisect.bisect_left(_g, r_idx)

        for g_idx in _g[start:]:

            diff = g_idx - r_idx

            num = b_ids[g_idx]

            if g_idx+diff in b_set:

                num -= 1

            res += num

    return res

            



ans = 0

for rgb in itertools.permutations((r, g, b), 3):

    ans += count_rgb(rgb[0], rgb[1], rgb[2])



print(ans)
import itertools
import bisect

N = int(eval(input()))
S = eval(input())
R_set = set()
G_set = set()
B_set = set()
R = []
G = []
B = []
R_ids = [0] * N
G_ids = [0] * N
B_ids = [0] * N
for i, s in enumerate(S):
    if s == "R":
        R_set.add(i)
        R.append(i)
        R_ids[i] = 1
    elif s == "G":
        G_set.add(i)
        G.append(i)
        G_ids[i] = 1
    else:
        B_set.add(i)
        B.append(i)
        B_ids[i] = 1
R_ids = list(itertools.accumulate(reversed(R_ids)))[::-1]
G_ids = list(itertools.accumulate(reversed(G_ids)))[::-1]
B_ids = list(itertools.accumulate(reversed(B_ids)))[::-1]
ans = 0
for r_idx in R:
    start = bisect.bisect_left(G, r_idx)
    for g_idx in G[start:]:
        diff = g_idx - r_idx
        num = B_ids[g_idx]
        if g_idx + diff in B_set:
            num -= 1
        ans += num
    start = bisect.bisect_left(B, r_idx)
    for b_idx in B[start:]:
        diff = b_idx - r_idx
        num = G_ids[b_idx]
        if b_idx + diff in G_set:
            num -= 1
        ans += num
for g_idx in G:
    start = bisect.bisect_left(R, g_idx)
    for r_idx in R[start:]:
        diff = r_idx - g_idx
        num = B_ids[r_idx]
        if r_idx + diff in B_set:
            num -= 1
        ans += num
    start = bisect.bisect_left(B, g_idx)
    for b_idx in B[start:]:
        diff = b_idx - g_idx
        num = R_ids[b_idx]
        if b_idx + diff in R_set:
            num -= 1
        ans += num
for b_idx in B:
    start = bisect.bisect_left(G, b_idx)
    for g_idx in G[start:]:
        diff = g_idx - b_idx
        num = R_ids[g_idx]
        if g_idx + diff in R_set:
            num -= 1
        ans += num
    start = bisect.bisect_left(R, b_idx)
    for r_idx in R[start:]:
        diff = r_idx - b_idx
        num = G_ids[r_idx]
        if r_idx + diff in G_set:
            num -= 1
        ans += num
print(ans)

import itertools
import bisect

N = int(eval(input()))
S = eval(input())
R_set = set()
G_set = set()
B_set = set()
R = []
G = []
B = []
R_ids = [0] * N
G_ids = [0] * N
B_ids = [0] * N
for i, s in enumerate(S):
    if s == "R":
        R_set.add(i)
        R.append(i)
        R_ids[i] = 1
    elif s == "G":
        G_set.add(i)
        G.append(i)
        G_ids[i] = 1
    else:
        B_set.add(i)
        B.append(i)
        B_ids[i] = 1
R_ids = list(itertools.accumulate(reversed(R_ids)))[::-1]
G_ids = list(itertools.accumulate(reversed(G_ids)))[::-1]
B_ids = list(itertools.accumulate(reversed(B_ids)))[::-1]
r = (R_set, R, R_ids)
g = (G_set, G, G_ids)
b = (B_set, B, B_ids)


def count_rgb(r, g, b):
    res = 0
    r_set, _r, r_ids = r
    g_set, _g, g_ids = g
    b_set, _b, b_ids = b
    for r_idx in _r:
        start = bisect.bisect_left(_g, r_idx)
        for g_idx in _g[start:]:
            diff = g_idx - r_idx
            num = b_ids[g_idx]
            if g_idx + diff in b_set:
                num -= 1
            res += num
    return res


ans = 0
for rgb in itertools.permutations((r, g, b), 3):
    ans += count_rgb(rgb[0], rgb[1], rgb[2])
print(ans)

+r = (R_set, R, R_ids) +g = (G_set, G, G_ids) +b = (B_set, B, B_ids) + + +def count_rgb(r, g, b): +    res = 0 +    r_set, _r, r_ids = r +    g_set, _g, g_ids = g +    b_set, _b, b_ids = b +    for r_idx in _r: +        start = bisect.bisect_left(_g, r_idx) +        for g_idx in _g[start:]: +            diff = g_idx - r_idx +            num = b_ids[g_idx] +            if g_idx + diff in b_set: +                num -= 1 +            res += num +    return res + + -for r_idx in R: -    start = bisect.bisect_left(G, r_idx) -    for g_idx in G[start:]: -        diff = g_idx - r_idx -        num = B_ids[g_idx] -        if g_idx + diff in B_set: -            num -= 1 -        ans += num -    start = bisect.bisect_left(B, r_idx) -    for b_idx in B[start:]: -        diff = b_idx - r_idx -        num = G_ids[b_idx] -        if b_idx + diff in G_set: -            num -= 1 -        ans += num -for g_idx in G: -    start = bisect.bisect_left(R, g_idx) -    for r_idx in R[start:]: -        diff = r_idx - g_idx -        num = B_ids[r_idx] -        if r_idx + diff in B_set: -            num -= 1 -        ans += num -    start = bisect.bisect_left(B, g_idx) -    for b_idx in B[start:]: -        diff = b_idx - g_idx -        num = R_ids[b_idx] -        if b_idx + diff in R_set: -            num -= 1 -        ans += num -for b_idx in B: -    start = bisect.bisect_left(G, b_idx) -    for g_idx in G[start:]: -        diff = g_idx - b_idx -        num = R_ids[g_idx] -        if g_idx + diff in R_set: -            num -= 1 -        ans += num -    start = bisect.bisect_left(R, b_idx) -    for r_idx in R[start:]: -        diff = r_idx - b_idx -        num = G_ids[r_idx] -        if r_idx + diff in G_set: -            num -= 1 -        ans += num +for rgb in itertools.permutations((r, g, b), 3): +    ans += count_rgb(rgb[0], rgb[1], rgb[2])