Given a tree, and the weights (in the form of strings) of all the nodes, the task is to count the nodes whose weighted string when concatenated with the strings of the sub-tree nodes becomes a pangram.
Pangram: A pangram is a sentence containing every letter of the English Alphabet.
Only the weighted string of sub-tree of node 1 makes the pangram.
Approach: Perform dfs on the tree and update the weight of every node such that it stores its weight concatenated with the weights of the sub-tree nodes. Then, count the nodes whose updated weighted string forms a pangram.
Below is the implementation of the above approach:
Time Complexity: O(N*S).
In dfs, every node of the tree is processed once, and hence the complexity due to the dfs is O(N) if there are total N nodes in the tree. Also, for processing each node the Pangram() function is used for every node which has a complexity of O(S) where S is the sum of the length of all weight strings in a subtree and since this is done for every node, the overall time complexity for this part becomes O(N*S). Therefore, the final time complexity is O(N*S).
Auxiliary Space: O(1).
Any extra space is not required, so the space complexity is constant.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Missing characters to make a string Pangram
- Count of pairs of Array elements which are divisible by K when concatenated
- Check if a String can be converted to Pangram in K changes
- Python program to check if given string is pangram
- C++ program to check whether a String is a Pangram or not
- Check if a given string is Pangram in Java
- Pangram Checking
- Using Set() in Python Pangram Checking
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Subtree of all nodes in a tree using DFS
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Count of nodes having odd divisors in the given subtree for Q queries
- Count of distinct colors in a subtree of a Colored Tree with given min frequency for Q queries
- Nth character in Concatenated Decimal String
- Concatenated string with uncommon characters of two strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.