Count of triplets from the given string with non | 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
function CountValidTriplet ( s , n ) { let count_a = 0 , count_b = 0 , count_c = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) count_a ++ ; if ( s [ i ] == ' ' ) count_b ++ ; if ( s [ i ] == ' ' ) count_c ++ ; } let Total_triplet = count_a * count_b * count_c ; 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 ) ; } let s = " " ; let n = s . length ; CountValidTriplet ( s , n ) ;