Count the nodes of the tree which make a pangram when concatenated with the sub | Java 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
import java . util . * ; class GFG { @ SuppressWarnings ( " unchecked " ) static Vector < Integer > [ ] graph = new Vector [ 100 ] ; static String [ ] weight = new String [ 100 ] ; static boolean Pangram ( String x ) { HashMap < Character , Integer > mp = new HashMap < > ( ) ; int n = x . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( x . charAt ( i ) ) ) { mp . put ( x . charAt ( i ) , mp . get ( x . charAt ( i ) ) + 1 ) ; } else { mp . put ( x . charAt ( i ) , 1 ) ; } } if ( mp . size ( ) == 26 ) return true ; else return false ; } static int countTotalPangram ( int n ) { int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( Pangram ( weight [ i ] ) ) cnt ++ ; return cnt ; } static void dfs ( int node , int parent ) { for ( int to : graph [ node ] ) { if ( to == parent ) continue ; dfs ( to , node ) ; weight [ node ] += weight [ to ] ; } } public static void main ( String [ ] args ) { int n = 6 ; weight [ 1 ] = " abcde " ; weight [ 2 ] = " fghijkl " ; weight [ 3 ] = " abcdefg " ; weight [ 4 ] = " mnopqr " ; weight [ 5 ] = " stuvwxy " ; weight [ 6 ] = " zabcdef " ; for ( int i = 0 ; i < graph . length ; i ++ ) graph [ i ] = new Vector < Integer > ( ) ; graph [ 1 ] . add ( 2 ) ; graph [ 2 ] . add ( 3 ) ; graph [ 2 ] . add ( 4 ) ; graph [ 1 ] . add ( 5 ) ; graph [ 5 ] . add ( 6 ) ; dfs ( 1 , 1 ) ; System . out . print ( countTotalPangram ( n ) ) ; } }