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