text: Count of substrings from given Ternary strings containing characters at least once | Function to count the number of substrings consists of 0 , 1 , and 2 ; Initialize frequency array of size 3 ; Stores the resultant count ; Traversing string str ; Update frequency array ; If all the characters are present counting number of substrings possible ; Update number of substrings ; Return the number of substrings ; Driver Code
code: function countSubstrings ( str ) { let freq = new Array ( 3 ) . fill ( 0 ) let count = 0 ; let i = 0 ; for ( let j = 0 ; j < str . length ; j ++ ) { freq [ str . charCodeAt ( j ) - ' ' . charCodeAt ( 0 ) ] ++ ; while ( freq [ 0 ] > 0 && freq [ 1 ] > 0 && freq [ 2 ] > 0 ) { freq [ str . charCodeAt ( i ++ ) - ' ' . charCodeAt ( 0 ) ] -- ; } count += i ; } return count ; } let str = " " ; let count = countSubstrings ( str ) ; document . write ( count ) ;