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).
Input: S = “RRGB”
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”
Explanation: No triplets exists.
- 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.
Time Complexity: O(N)
Auxiliary Space Cpmplexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Restore original String from given Encrypted String by the given operations
- Count of Distinct strings possible by inserting K characters in the original string
- Count of triplets from the given string with non-equidistant characters
- Decode a median string to the original string
- Count ordered pairs of numbers with a given LCM
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Count Derangements (Permutation such that no element appears in its original position)
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
- Count number of triplets with product equal to given number with duplicates allowed
- Count of triplets in an array that satisfy the given conditions
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count number of ordered pairs with Even and Odd Sums
- Count number of ordered pairs with Even and Odd Product
- Count ordered pairs with product less than N
- Count of Ordered Pairs (X, Y) satisfying the Equation 1/X + 1/Y = 1/N
- Print n smallest elements from given array in their original order
- Minimum time to return array to its original state after given modifications
- Find the original matrix when largest element in a row and a column are given
- Find the original coordinates whose Manhattan distances are given
- Find the deleted value from the array when average of original elements is given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.