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, CS.length );
	foreach_reverse( i, c; s )
	{
		foreach( j; 0L .. tss[ i ].length.signed )
		{
			tss[ i ][ j ] = tss[ i + 1 ][ j ] + ( j == CS[ 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 = CS.length.signed - CS[ s[ i ] ] - CS[ s[ j ] ];
			ans += tss[ j + 1 ][ c ] - ( j + d < s.length && CS[ s[ j + d ] ] == c );
		}
	}
	return ans;
}
