Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
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.
Examples:
Input:
Output: 1
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:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > graph[100];
vector<string> weight(100);
bool Pangram(string x)
{
map< char , int > mp;
int n = x.size();
for ( int i = 0; i < n; i++)
mp[x[i]]++;
if (mp.size() == 26)
return true ;
else
return false ;
}
int countTotalPangram( int n)
{
int cnt = 0;
for ( int i = 1; i <= n; i++)
if (Pangram(weight[i]))
cnt++;
return cnt;
}
void dfs( int node, int parent)
{
for ( int to : graph[node]) {
if (to == parent)
continue ;
dfs(to, node);
weight[node] += weight[to];
}
}
int main()
{
int n = 6;
weight[1] = "abcde" ;
weight[2] = "fghijkl" ;
weight[3] = "abcdefg" ;
weight[4] = "mnopqr" ;
weight[5] = "stuvwxy" ;
weight[6] = "zabcdef" ;
graph[1].push_back(2);
graph[2].push_back(3);
graph[2].push_back(4);
graph[1].push_back(5);
graph[5].push_back(6);
dfs(1, 1);
cout << countTotalPangram(n);
return 0;
}
|
Java
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));
}
}
|
Python3
graph = [[] for i in range ( 100 )]
weight = [ 0 ] * 100
def Pangram(x):
mp = {}
n = len (x)
for i in range (n):
if x[i] not in mp:
mp[x[i]] = 0
mp[x[i]] + = 1
if ( len (mp) = = 26 ):
return True
else :
return False
def countTotalPangram(n):
cnt = 0
for i in range ( 1 , n + 1 ):
if (Pangram(weight[i])):
cnt + = 1
return cnt
def dfs(node, parent):
for to in graph[node]:
if (to = = parent):
continue
dfs(to, node)
weight[node] + = weight[to]
n = 6
weight[ 1 ] = "abcde"
weight[ 2 ] = "fghijkl"
weight[ 3 ] = "abcdefg"
weight[ 4 ] = "mnopqr"
weight[ 5 ] = "stuvwxy"
weight[ 6 ] = "zabcdef"
graph[ 1 ].append( 2 )
graph[ 2 ].append( 3 )
graph[ 2 ].append( 4 )
graph[ 1 ].append( 5 )
graph[ 5 ].append( 6 )
dfs( 1 , 1 )
print (countTotalPangram(n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int > []graph =
new List< int >[100];
static String []weight =
new String[100];
static bool Pangram(String x)
{
Dictionary< char ,
int > mp = new Dictionary< char ,
int >();
int n = x.Length;
for ( int i = 0 ; i < n; i++)
{
if (mp.ContainsKey(x[i]))
{
mp[x[i]] = mp[x[i]] + 1;
}
else
{
mp.Add(x[i], 1);
}
}
if (mp.Count == 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)
{
foreach ( int to in 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 List< int >();
graph[1].Add(2);
graph[2].Add(3);
graph[2].Add(4);
graph[1].Add(5);
graph[5].Add(6);
dfs(1, 1);
Console.Write(countTotalPangram(n));
}
}
|
Javascript
<script>
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] = "abcde" ;
weight[2] = "fghijkl" ;
weight[3] = "abcdefg" ;
weight[4] = "mnopqr" ;
weight[5] = "stuvwxy" ;
weight[6] = "zabcdef" ;
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));
</script>
|
Complexity Analysis:
- 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.
Last Updated :
08 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...