Open In App

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

Last Updated : 19 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 
 

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++




// 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 count = 0;
    int n = color.length();
 
    // generate all possible ordered triplets of (R, G, B)
    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') {
                            // increment the count if the triplet (R, G, B) occurs in the string
                            count++;
                        }
                    }
                }
            }
        }
    }
 
    return count;
}
 
 
// Driver program
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();
 
        // generate all possible ordered triplets of (R, G, B)
        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') {
                                // increment the count if the triplet (R, G, B) occurs in the string
                                count++;
                            }
                        }
                    }
                }
            }
        }
 
        return count;
    }
 
    public static void main(String[] args) {
        String color = "RRGGBBRGGBB";
        System.out.println(countTriplets(color));
    }
}


Python3




# function to count the
# ordered triplets (R, G, B)
def countTriplets(color):
    count = 0
    n = len(color)
 
    # generate all possible ordered triplets of (R, G, B)
    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':
                            # increment the count if the triplet (R, G, B) occurs in
                            # the string
                            count += 1
 
    return count
 
# Driver program
color = "RRGGBBRGGBB"
print(countTriplets(color))


C#




using System;
 
public class Program {
    // function to count the ordered triplets (R, G, B)
    public static int CountTriplets(string color)
    {
        int count = 0;
        int n = color.Length;
 
        // generate all possible ordered triplets of (R, G,
        // B)
        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') {
                                // increment the count if
                                // the triplet (R, G, B)
                                // occurs in the string
                                count++;
                            }
                        }
                    }
                }
            }
        }
 
        return count;
    }
 
    // Driver program
    public static void Main()
    {
        string color = "RRGGBBRGGBB";
        Console.WriteLine(CountTriplets(color));
    }
}
 
// This code is contributed by Prajwal Kandekar


Javascript




// function to count the
// ordered triplets (R, G, B)
function countTriplets(color) {
    let count = 0;
    const n = color.length;
 
    // generate all possible ordered triplets of (R, G, B)
    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') {
              // increment the count if the triplet (R, G, B) occurs in the string
                            count++;
                        }
                    }
                }
            }
        }
    }
 
    return count;
}
 
// Driver program
const color = "RRGGBBRGGBB";
console.log(countTriplets(color));


Output

28

Time Complexity: O(N^3)

Auxiliary Space: O(1)

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 characters 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 <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;
}


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




<script>
 
// Javascript code for the above program
 
// function to count the
// ordered triplets (R, G, B)
function countTriplets(color)
{
    var result = 0, Blue_Count = 0;
    var Red_Count = 0;
 
    var data = color.split('');
 
    // count the B(blue) colour
    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;
}
 
// Driver program
var color = "RRGGBBRGGBB";
document.write( countTriplets(color));
 
</script>


Output

28

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads