unittest
{
	assert( [ "4", "RRGB" ].parse.expand.solve == 1 );
	assert( [ "39", "RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB" ].parse.expand.solve == 1800 );
}



import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	input.popFront;
	auto s = input.front.to!( dchar[] );
	return tuple( s );
}

auto solve( dchar[] s )
{
	const long[ dchar ] CS = [ 'R':0L, 'G':1L, 'B':2L ];
	
	auto tss = new long[][]( 1 + s.length, 3 );
	foreach( ref ts, c; lockstep( tss, s ) )
	{
		ts[ CS[ c ] ]++;
	}
	foreach_reverse( i; 1L .. tss.length.signed )
	{
		foreach( c; 0L .. tss[ i ].length.signed )
		{
			tss[ i - 1 ][ c ] += tss[ i ][ c ];
		}
	}
	
	auto ans = 0L;
	foreach( i; 0L .. tss.length.signed - 3 )
	{
		foreach( j; i + 1 .. tss.length.signed - 2 )
		{
			if( s[ i ] == s[ j ] ) continue;
			
			auto d = j - i;
			auto c = 0L;
			for( ; c < CS.length; c++ )
			{
				if( c != CS[ s[ i ] ] && c != CS[ s[ j ] ] ) break;
			}
			ans += tss[ j + 1 ][ c ] - ( j + d < s.length && CS[ s[ j + d ] ] == c );
		}
	}
	return ans;
}
