If all the characters are present counting number of substrings possible
while ( freq [ 0 ] > 0 && freq [ 1 ] > 0 && freq [ 2 ] > 0 ) { freq [ str . charCodeAt ( i ++ ) - ' ' . charCodeAt ( 0 ) ] -- ; }