Skip to content
Related Articles

Related Articles

Improve Article
Count of triplets from the given string with non-equidistant characters
  • Last Updated : 15 Jun, 2021

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



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 <bits/stdc++.h>
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

Javascript




<script>
 
// Javascript program to count of triplets
// from the given string with
// non-equidistant characters
 
// Function to count valid triplets
function CountValidTriplet(s, n)
{
     
    // Store frequencies of a, b and c
    let count_a = 0,
        count_b = 0,
        count_c = 0;
 
    for(let 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
    let Total_triplet = count_a * count_b * count_c;
 
    // Subtract invalid triplets
    for(let i = 0; i < n; i++)
    {
        for(let 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--;
        }
    }
    document.write(Total_triplet);
}
 
// Driver code
let s = "abcbcabc";
let n = s.length;
   
CountValidTriplet(s, n);
 
// This code is contributed by mukesh07
 
</script>
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :