#![allow(non_snake_case)]
#![allow(unused_imports)]
use proconio::input;
use std::cmp::max;
use std::cmp::min;
use std::collections::HashMap;
use std::collections::VecDeque;

// O(N^3)
#[allow(dead_code)]
fn solve_naive(N: usize, S: &str) -> i64 {
    let mut ans = 0i64;

    for i in 0..N {
        for j in i + 1..N {
            if &S[i..i + 1] == &S[j..j + 1] {
                continue;
            }

            for k in j + 1..N {
                if &S[i..i + 1] == &S[k..k + 1] || &S[j..j + 1] == &S[k..k + 1] {
                    continue;
                }

                if j - i != k - j {
                    ans += 1;
                }
            }
        }
    }

    return ans;
}

// O(N^2)だと思っていたがTLEした。文字列のSliceが遅い?
#[allow(dead_code)]
fn solve(N: usize, S: &str) -> i64 {
    let mut ans = 0i64;

    let mut r_acc_sum: Vec<i64> = vec![0; N + 1];
    let mut g_acc_sum: Vec<i64> = vec![0; N + 1];
    let mut b_acc_sum: Vec<i64> = vec![0; N + 1];

    for i in 0..N {
        if &S[i..i + 1] == "R" {
            r_acc_sum[i + 1] = r_acc_sum[i] + 1;
            g_acc_sum[i + 1] = g_acc_sum[i];
            b_acc_sum[i + 1] = b_acc_sum[i];
        } else if &S[i..i + 1] == "G" {
            r_acc_sum[i + 1] = r_acc_sum[i];
            g_acc_sum[i + 1] = g_acc_sum[i] + 1;
            b_acc_sum[i + 1] = b_acc_sum[i];
        } else if &S[i..i + 1] == "B" {
            r_acc_sum[i + 1] = r_acc_sum[i];
            g_acc_sum[i + 1] = g_acc_sum[i];
            b_acc_sum[i + 1] = b_acc_sum[i] + 1;
        }
    }

    // eprintln!("{:?}", r_acc_sum);
    // eprintln!("{:?}", g_acc_sum);
    // eprintln!("{:?}", b_acc_sum);

    for i in 0..N {
        let s1: &str = &S[i..i + 1];
        for j in i + 1..N {
            let s2: &str = &S[j..j + 1];
            if s1 == s2 {
                continue;
            }

            // 残りのkは(j+1..N)のうち何個選べるか?
            // (j-1) - (i-1) == (k-1) - (j-1) であるものがあれば除外
            // k = 2 * j - i
            let ignored_k = 2 * j - i;

            if (s1 == "G" && s2 == "B") || (s1 == "B" && s2 == "G") {
                let cnt = r_acc_sum[N] - r_acc_sum[j];
                // eprintln!("{} {} -> {}", i, j, cnt);
                ans += cnt;

                if cnt > 0 && j < ignored_k && ignored_k < N {
                    let s_ig = &S[ignored_k..ignored_k + 1];
                    if s_ig == "R" {
                        // eprintln!("ignored: {} {} {}", i, j, ignored_k);
                        ans -= 1;
                    }
                }
            } else if (s1 == "R" && s2 == "B") || (s1 == "B" && s2 == "R") {
                let cnt = g_acc_sum[N] - g_acc_sum[j];
                // eprintln!("{} {} -> {}", i, j, cnt);
                ans += cnt;

                if cnt > 0 && j < ignored_k && ignored_k < N {
                    let s_ig = &S[ignored_k..ignored_k + 1];
                    if s_ig == "G" {
                        // eprintln!("ignored: {} {} {}", i, j, ignored_k);
                        ans -= 1;
                    }
                }
            } else if (s1 == "R" && s2 == "G") || (s1 == "G" && s2 == "R") {
                let cnt = b_acc_sum[N] - b_acc_sum[j];
                // eprintln!("{} {} -> {}", i, j, cnt);
                ans += cnt;

                if cnt > 0 && j < ignored_k && ignored_k < N {
                    let s_ig = &S[ignored_k..ignored_k + 1];
                    if s_ig == "B" {
                        // eprintln!("ignored: {} {} {}", i, j, ignored_k);
                        ans -= 1;
                    }
                }
            }
        }
    }

    return ans;
}

fn main() {
    input! {
        N: usize,
        S: String,
    }
    let ans = solve(N, &S);
    println!("{}", ans);
}


Measured memory_bytes: 2248