# Count of ordered triplets (R, G, B) in a given original string

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:2

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:0

Explanation:No triplets exists.

**Approach:**

- Count the number of B(Blue) in the given string and store the value in a
**Blue_Count**variable. - Initialize
**Red_Count**= 0. - Iterate over all character of string from left to right.
- If the current character is (R)red, increase the
**Red_Count**. - If current character is (B)blue, decrease
**Blue_Count**. - 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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

28

**Time Complexity: ** *O(N)*

**Auxillary Space Cpmplexity: *** O(1)*

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count of triplets from the given string with non-equidistant characters
- Decode a median string to the original string
- Count ordered pairs with product less than N
- Count number of ordered pairs with Even and Odd Sums
- Count number of ordered pairs with Even and Odd Product
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count Derangements (Permutation such that no element appears in its original position)
- Count the triplets such that A[i] < B[j] < C[k]
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
- Count non-negative triplets with sum equal to N
- Count Triplets such that one of the numbers can be written as sum of the other two
- Count of triplets in an array that satisfy the given conditions
- Count of triplets of numbers 1 to N such that middle element is always largest
- Count ways to form minimum product triplets
- Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Check if count of Alphabets and count of Numbers are equal in the given String
- Find the count of palindromic sub-string of a string in its sorted form

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.