Count of ordered triplets ( R , G , B ) in a given original string | Java code for the above program ; function to count the ordered triplets ( R , G , B ) ; count the B ( blue ) colour ; Driver Code
class GFG { static int countTriplets ( String color ) { int result = 0 , Blue_Count = 0 ; int Red_Count = 0 ; int len = color . length ( ) ; int i ; for ( i = 0 ; i < len ; i ++ ) { if ( color . charAt ( i ) == ' B ' ) Blue_Count ++ ; } for ( i = 0 ; i < len ; i ++ ) { if ( color . charAt ( i ) == ' B ' ) Blue_Count -- ; if ( color . charAt ( i ) == ' R ' ) Red_Count ++ ; if ( color . charAt ( i ) == ' G ' ) result += Red_Count * Blue_Count ; } return result ; } public static void main ( String [ ] args ) { String color = " RRGGBBRGGBB " ; System . out . println ( countTriplets ( color ) ) ; } }