Count of ordered triplets (R, G, B) in a given original string
Last Updated :
19 Apr, 2023
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.
Brute Force Approach:
The brute force approach to solve this problem would be to generate all possible ordered triplets of (R, G, B) from the given string and count how many of them occur in the string. To generate all possible triplets, we can use three nested loops, where the first loop iterates over all possible positions for the R color, the second loop iterates over all possible positions for the G color after the R color, and the third loop iterates over all possible positions for the B color after the G color. Then, we can check whether the triplet (R, G, B) occurs in the string by checking whether the three corresponding characters are R, G, and B, respectively.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets(string color)
{
int count = 0;
int n = color.length();
for ( int i = 0; i < n - 2; i++) {
if (color[i] == 'R' ) {
for ( int j = i + 1; j < n - 1; j++) {
if (color[j] == 'G' ) {
for ( int k = j + 1; k < n; k++) {
if (color[k] == 'B' ) {
count++;
}
}
}
}
}
}
return count;
}
int main()
{
string color = "RRGGBBRGGBB" ;
cout << countTriplets(color);
return 0;
}
|
Java
import java.util.*;
class Main {
public static int countTriplets(String color) {
int count = 0 ;
int n = color.length();
for ( int i = 0 ; i < n - 2 ; i++) {
if (color.charAt(i) == 'R' ) {
for ( int j = i + 1 ; j < n - 1 ; j++) {
if (color.charAt(j) == 'G' ) {
for ( int k = j + 1 ; k < n; k++) {
if (color.charAt(k) == 'B' ) {
count++;
}
}
}
}
}
}
return count;
}
public static void main(String[] args) {
String color = "RRGGBBRGGBB" ;
System.out.println(countTriplets(color));
}
}
|
Python3
def countTriplets(color):
count = 0
n = len (color)
for i in range (n - 2 ):
if color[i] = = 'R' :
for j in range (i + 1 , n - 1 ):
if color[j] = = 'G' :
for k in range (j + 1 , n):
if color[k] = = 'B' :
count + = 1
return count
color = "RRGGBBRGGBB"
print (countTriplets(color))
|
C#
using System;
public class Program {
public static int CountTriplets( string color)
{
int count = 0;
int n = color.Length;
for ( int i = 0; i < n - 2; i++) {
if (color[i] == 'R' ) {
for ( int j = i + 1; j < n - 1; j++) {
if (color[j] == 'G' ) {
for ( int k = j + 1; k < n; k++) {
if (color[k] == 'B' ) {
count++;
}
}
}
}
}
}
return count;
}
public static void Main()
{
string color = "RRGGBBRGGBB" ;
Console.WriteLine(CountTriplets(color));
}
}
|
Javascript
function countTriplets(color) {
let count = 0;
const n = color.length;
for (let i = 0; i < n - 2; i++) {
if (color[i] === 'R' ) {
for (let j = i + 1; j < n - 1; j++) {
if (color[j] === 'G' ) {
for (let k = j + 1; k < n; k++) {
if (color[k] === 'B' ) {
count++;
}
}
}
}
}
}
return count;
}
const color = "RRGGBBRGGBB" ;
console.log(countTriplets(color));
|
Time Complexity: O(N^3)
Auxiliary Space: O(1)
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 characters 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++
#include <bits/stdc++.h>
using namespace std;
int countTriplets(string color)
{
int result = 0, Blue_Count = 0;
int Red_Count = 0;
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;
}
int main()
{
string color = "RRGGBBRGGBB" ;
cout << countTriplets(color);
return 0;
}
|
Java
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));
}
}
|
Python3
def countTriplets(color) :
result = 0 ; Blue_Count = 0 ;
Red_Count = 0 ;
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;
if __name__ = = "__main__" :
color = "RRGGBBRGGBB" ;
print (countTriplets(color));
|
C#
using System;
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[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;
}
public static void Main( string [] args)
{
string color = "RRGGBBRGGBB" ;
Console.WriteLine(countTriplets(color));
}
}
|
Javascript
<script>
function countTriplets(color)
{
var result = 0, Blue_Count = 0;
var Red_Count = 0;
var data = color.split( '' );
for ( var i=0;i<data.length;i++)
{
if (data[i] == 'B' )
Blue_Count++;
}
for ( var i=0;i<data.length;i++){
if (data[i] == 'B' )
Blue_Count--;
if (data[i] == 'R' )
Red_Count++;
if (data[i] == 'G' )
result += Red_Count * Blue_Count;
}
return result;
}
var color = "RRGGBBRGGBB" ;
document.write( countTriplets(color));
</script>
|
Time Complexity: O(N)
Auxiliary Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...