# Count of triplets from the given string with non-equidistant characters

Given a string s containing letters ‘a’, ‘b’ and ‘c’, the task is to calculate all possible triplets made up of the three characters, and the distance between them should not be the same.

Distance between two letters is basically the difference between their indices.

Examples:

Input: s = “aabc”
Output: 1
Explanation:
Two possible triplets comprising of ‘a’, ‘b’ and ‘c’ are {1, 3, 4} and { 2, 3, 4}.
But the characters in the second triplet are equidistant. Hence, there is only one possible triplet satisfying the above condition.

Input: s = “abcbcabc”
Output: 13

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
To solve the problem mentioned above, we need to follow the steps given below:

• Count all the occurrences of letters ‘a’, ‘b’, ‘c’ in the given string s.
• Find all triplets containing a, b, c by multiplying their respective counts.
• After this subtract all those triplets which has same distance between indices and print the resultant output.

Below is the implementation of the above approach:

## C++

 `// C++ Program to count of triplets ` `// from the given string with ` `// non-equidistant characters ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to count valid triplets ` `void` `CountValidTriplet(string s, ``int` `n) ` `{ ` `    ``// Store frequencies of a, b and c ` `    ``int` `count_a = 0, ` `        ``count_b = 0, ` `        ``count_c = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// If the current letter is 'a' ` `        ``if` `(s[i] == ``'a'``) ` `            ``count_a++; ` ` `  `        ``// If the current letter is 'b' ` `        ``if` `(s[i] == ``'b'``) ` `            ``count_b++; ` ` `  `        ``// If the current letter is 'c' ` `        ``if` `(s[i] == ``'c'``) ` `            ``count_c++; ` `    ``} ` ` `  `    ``// Calculate total no of triplets ` `    ``int` `Total_triplet = count_a ` `                        ``* count_b ` `                        ``* count_c; ` ` `  `    ``// Subtract invalid triplets ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``for` `(``int` `j = i + 1; j < n; j++) { ` ` `  `            ``if` `((2 * j - i) < n ` `                ``&& s[j] != s[i] ` `                ``&& s[j * 2 - i] != s[j] ` `                ``&& s[2 * j - i] != s[i]) ` ` `  `                ``Total_triplet--; ` `        ``} ` `    ``} ` `    ``cout << Total_triplet; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``string s = ``"abcbcabc"``; ` ` `  `    ``int` `n = s.length(); ` ` `  `    ``CountValidTriplet(s, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to count of triplets ` `// from the given string with ` `// non-equidistant characters ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to count valid triplets ` `static` `void` `CountValidTriplet(String s, ``int` `n) ` `{ ` `     `  `    ``// Store frequencies of a, b and c ` `    ``int` `count_a = ``0``, ` `        ``count_b = ``0``, ` `        ``count_c = ``0``; ` ` `  `    ``for``(``int` `i = ``0``; i < n; i++)  ` `    ``{ ` `        `  `       ``// If the current letter is 'a' ` `       ``if` `(s.charAt(i) == ``'a'``) ` `           ``count_a++; ` `        `  `       ``// If the current letter is 'b' ` `       ``if` `(s.charAt(i) == ``'b'``) ` `           ``count_b++; ` `        `  `       ``// If the current letter is 'c' ` `       ``if` `(s.charAt(i) == ``'c'``) ` `           ``count_c++; ` `    ``} ` `     `  `    ``// Calculate total no of triplets ` `    ``int` `Total_triplet = count_a * count_b * count_c; ` `     `  `    ``// Subtract invalid triplets ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `       ``for``(``int` `j = i + ``1``; j < n; j++) ` `       ``{ ` `          ``if` `((``2` `* j - i) < n &&  ` `              ``s.charAt(j) != s.charAt(i) &&  ` `              ``s.charAt(j * ``2` `- i) != s.charAt(j) &&  ` `              ``s.charAt(``2` `* j - i) != s.charAt(i)) ` `              ``Total_triplet--; ` `       ``} ` `    ``} ` `    ``System.out.println(Total_triplet); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``String s = ``"abcbcabc"``; ` `    ``int` `n = s.length(); ` `     `  `    ``CountValidTriplet(s, n); ` `} ` `} ` ` `  `// This code is contributed by offbeat `

## Python3

 `# Python3 program to count of triplets  ` `# from the given string with  ` `# non-equidistant characters  ` ` `  `# Function to count valid triplets  ` `def` `CountValidTriplet(s, n): ` `     `  `    ``# Store frequencies of a, b and c  ` `    ``count_a ``=` `0` `    ``count_b ``=` `0` `    ``count_c ``=` `0` `     `  `    ``for` `i ``in` `range``(n): ` `         `  `        ``# If the current letter is 'a'  ` `        ``if` `(s[i] ``=``=` `'a'``): ` `            ``count_a ``+``=` `1` ` `  `        ``# If the current letter is 'b'  ` `        ``if` `(s[i] ``=``=` `'b'``): ` `            ``count_b ``+``=` `1` ` `  `        ``# If the current letter is 'c'  ` `        ``if` `(s[i] ``=``=` `'c'``): ` `            ``count_c ``+``=` `1` ` `  `    ``# Calculate total no of triplets  ` `    ``Total_triplet ``=` `count_a ``*` `count_b ``*` `count_c ` ` `  `    ``# Subtract invalid triplets  ` `    ``for` `i ``in` `range``(n): ` `        ``for` `j ``in` `range``(i ``+` `1``, n): ` `             `  `            ``if` `((``2` `*` `j ``-` `i) < n ``and`  `                   ``s[j] !``=` `s[i] ``and`  `                   ``s[j ``*` `2` `-` `i] !``=` `s[j] ``and`  `                   ``s[``2` `*` `j ``-` `i] !``=` `s[i]): ` `                 `  `                ``Total_triplet ``-``=` `1` `                 `  `    ``print``(Total_triplet) ` `     `  `# Driver Code  ` `s ``=` `"abcbcabc"` `n ``=` `len``(s) ` `CountValidTriplet(s, n) ` ` `  `# This code is contributed by yatinagg `

## C#

 `// C# program to count of triplets ` `// from the given string with ` `// non-equidistant characters ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to count valid triplets ` `static` `void` `CountValidTriplet(``string` `s, ``int` `n) ` `{ ` `     `  `    ``// Store frequencies of a, b and c ` `    ``int` `count_a = 0, ` `        ``count_b = 0, ` `        ``count_c = 0; ` ` `  `    ``for``(``int` `i = 0; i < n; i++)  ` `    ``{ ` `       ``// If the current letter is 'a' ` `       ``if` `(s[i] == ``'a'``) ` `           ``count_a++; ` `             `  `       ``// If the current letter is 'b' ` `       ``if` `(s[i] == ``'b'``) ` `           ``count_b++; ` `             `  `       ``// If the current letter is 'c' ` `       ``if` `(s[i] == ``'c'``) ` `           ``count_c++; ` `    ``} ` `     `  `    ``// Calculate total no of triplets ` `    ``int` `Total_triplet = count_a * count_b * count_c; ` `     `  `    ``// Subtract invalid triplets ` `    ``for``(``int` `i = 0; i < n; i++) ` `    ``{ ` `       ``for``(``int` `j = i + 1; j < n; j++) ` `       ``{ ` `          ``if` `((2 * j - i) < n &&  ` `              ``s[j] != s[i] &&  ` `              ``s[j * 2 - i] != s[j] &&  ` `              ``s[2 * j - i] != s[i]) ` `              ``Total_triplet--; ` `       ``} ` `    ``} ` `    ``Console.Write(Total_triplet); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``string` `s = ``"abcbcabc"``; ` `    ``int` `n = s.Length; ` `     `  `    ``CountValidTriplet(s, n); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

Output:

```13
```

Time complexity: O(N2)

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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.