Count the nodes of the tree which make a pangram when concatenated with the sub | JavaScript implementation of the approach ; Function that returns if the string x is a pangram ; Function to return the count of nodes which make pangram with the sub - tree nodes ; Function to perform dfs and update the nodes such that weight [ i ] will store the weight [ i ] concatenated with the weights of all the nodes in the sub - tree ; Driver code ; Weights of the nodes ; Edges of the tree
let graph = new Array ( ) ; for ( let i = 0 ; i < 100 ; i ++ ) { graph . push ( [ ] ) } let weight = new Array ( 100 ) . fill ( 0 ) ; function Pangram ( x ) { let mp = new Map ( ) ; let n = x . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( x [ i ] ) ) { mp . set ( x [ i ] , mp . get ( x [ i ] ) + 1 ) } else { mp . set ( x [ i ] , 1 ) } } if ( mp . size == 26 ) return true ; else return false ; } function countTotalPangram ( n ) { let cnt = 0 ; for ( let i = 1 ; i <= n ; i ++ ) if ( Pangram ( weight [ i ] ) ) cnt ++ ; return cnt ; } function dfs ( node , parent ) { for ( let to of graph [ node ] ) { if ( to == parent ) continue ; dfs ( to , node ) ; weight [ node ] += weight [ to ] ; } } let n = 6 ; weight [ 1 ] = " " ; weight [ 2 ] = " " ; weight [ 3 ] = " " ; weight [ 4 ] = " " ; weight [ 5 ] = " " ; weight [ 6 ] = " " ; graph [ 1 ] . push ( 2 ) ; graph [ 2 ] . push ( 3 ) ; graph [ 2 ] . push ( 4 ) ; graph [ 1 ] . push ( 5 ) ; graph [ 5 ] . push ( 6 ) ; dfs ( 1 , 1 ) ; document . write ( countTotalPangram ( n ) ) ;