Related Articles
Count of ordered triplets (R, G, B) in a given original string
• Difficulty Level : Easy
• Last Updated : 05 May, 2021

Given a string S of size N, the task is to count all possible triplets in the given string which consists of only 3 colours (R)Red, (G)Green and (B)Blue in the order (R, G, B).
Examples:

Input: S = “RRGB”
Output:
Explanation: There are two triplets of RGB in the given string:

• R at index 0, G at index 2 and B at index 3 forms one triplet of RGB.
• R at index 1, G at index 2 and B at index 3 forms the second triplet of RGB.

Input: S = “GBR”
Output:
Explanation: No triplets exists.

Approach:

1. Count the number of B(Blue) in the given string and store the value in a Blue_Count variable.
2. Initialize Red_Count = 0.
3. Iterate over all character of string from left to right.
4. If the current character is (R)red, increase the Red_Count.
5. If current character is (B)blue, decrease Blue_Count.
6. If the current character is G(green) add Red_Count * Blue_Count in the result.

Below is the implementation of the above approach.

## C++

 `// C++ code for the above program` `#include ``using` `namespace` `std;` `// function to count the``// ordered triplets (R, G, B)``int` `countTriplets(string color)``{``    ``int` `result = 0, Blue_Count = 0;``    ``int` `Red_Count = 0;` `    ``// count the B(blue) colour``    ``for` `(``char` `c : color) {``        ``if` `(c == ``'B'``)``            ``Blue_Count++;``    ``}` `    ``for` `(``char` `c : color) {``        ``if` `(c == ``'B'``)``            ``Blue_Count--;``        ``if` `(c == ``'R'``)``            ``Red_Count++;``        ``if` `(c == ``'G'``)``            ``result += Red_Count * Blue_Count;``    ``}``    ``return` `result;``}` `// Driver program``int` `main()``{``    ``string color = ``"RRGGBBRGGBB"``;``    ``cout << countTriplets(color);``    ``return` `0;``}`

## Java

 `// Java code for the above program``class` `GFG{``        ` `// function to count the``// ordered triplets (R, G, B)``static` `int` `countTriplets(String color)``{``    ``int` `result = ``0``, Blue_Count = ``0``;``    ``int` `Red_Count = ``0``;``    ``int` `len = color.length();``    ``int` `i;``    ` `    ``// count the B(blue) colour``    ``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;``}` `// Driver Code``public` `static` `void` `main (String[] args)``{``    ``String color = ``"RRGGBBRGGBB"``;``    ``System.out.println(countTriplets(color));``}` `}` `// This code is contributed by AnkitRai01`

## Python3

 `# Python3 code for the above program` `# function to count the``# ordered triplets (R, G, B)``def` `countTriplets(color) :` `    ``result ``=` `0``; Blue_Count ``=` `0``;``    ``Red_Count ``=` `0``;` `    ``# count the B(blue) colour``    ``for` `c ``in` `color :``        ``if` `(c ``=``=` `'B'``) :``            ``Blue_Count ``+``=` `1``;` `    ``for` `c ``in` `color :``        ``if` `(c ``=``=` `'B'``) :``            ``Blue_Count ``-``=` `1``;``            ` `        ``if` `(c ``=``=` `'R'``) :``            ``Red_Count ``+``=` `1``;``            ` `        ``if` `(c ``=``=` `'G'``) :``            ``result ``+``=` `Red_Count ``*` `Blue_Count;``    ` `    ``return` `result;` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``color ``=` `"RRGGBBRGGBB"``;``    ``print``(countTriplets(color));` `# This code is contributed by AnkitRai01`

## C#

 `// C# code for the above program``using` `System;``class` `GFG{``        ` `// function to count the``// ordered triplets (R, G, B)``static` `int` `countTriplets(String color)``{``    ``int` `result = 0, Blue_Count = 0;``    ``int` `Red_Count = 0;``    ``int` `len = color.Length;``    ``int` `i;` `    ``// count the B(blue) colour``    ``for` `(i = 0; i < len ; i++)``    ``{``        ``if` `(color[i] == ``'B'``)``            ``Blue_Count++;``    ``}` `    ``for` `(i = 0; i < len ; i++)``    ``{``        ``if` `(color[i] == ``'B'``)``            ``Blue_Count--;``        ``if` `(color[i] == ``'R'``)``            ``Red_Count++;``        ``if` `(color[i] == ``'G'``)``            ``result += Red_Count * Blue_Count;``    ``}``    ``return` `result;``}` `// Driver Code``public` `static` `void` `Main(``string``[] args)``{``    ``string` `color = ``"RRGGBBRGGBB"``;``    ``Console.WriteLine(countTriplets(color));``}``}` `// This code is contributed by AnkitRai01`

## Javascript

 ``
Output:
`28`

Time Complexity: O(N)
Auxiliary Space Cpmplexity: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up